home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.4 / lib-tk / Tkinter.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  179.2 KB  |  4,767 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Wrapper functions for Tcl/Tk.
  5.  
  6. Tkinter provides classes which allow the display, positioning and
  7. control of widgets. Toplevel widgets are Tk and Toplevel. Other
  8. widgets are Frame, Label, Entry, Text, Canvas, Button, Radiobutton,
  9. Checkbutton, Scale, Listbox, Scrollbar, OptionMenu, Spinbox
  10. LabelFrame and PanedWindow.
  11.  
  12. Properties of the widgets are specified with keyword arguments.
  13. Keyword arguments have the same name as the corresponding resource
  14. under Tk.
  15.  
  16. Widgets are positioned with one of the geometry managers Place, Pack
  17. or Grid. These managers can be called with methods place, pack, grid
  18. available in every Widget.
  19.  
  20. Actions are bound to events by resources (e.g. keyword argument
  21. command) or with the method bind.
  22.  
  23. Example (Hello, World):
  24. import Tkinter
  25. from Tkconstants import *
  26. tk = Tkinter.Tk()
  27. frame = Tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
  28. frame.pack(fill=BOTH,expand=1)
  29. label = Tkinter.Label(frame, text="Hello, World")
  30. label.pack(fill=X, expand=1)
  31. button = Tkinter.Button(frame,text="Exit",command=tk.destroy)
  32. button.pack(side=BOTTOM)
  33. tk.mainloop()
  34. '''
  35. __version__ = '$Revision: 39220 $'
  36. import sys
  37. if sys.platform == 'win32':
  38.     import FixTk
  39.  
  40.  
  41. try:
  42.     import _tkinter
  43. except ImportError:
  44.     msg = None
  45.     raise ImportError, str(msg) + ', please install the python-tk package'
  46.  
  47. tkinter = _tkinter
  48. TclError = _tkinter.TclError
  49. from types import *
  50. from Tkconstants import *
  51.  
  52. try:
  53.     import MacOS
  54.     _MacOS = MacOS
  55.     del MacOS
  56. except ImportError:
  57.     _MacOS = None
  58.  
  59. wantobjects = 1
  60. TkVersion = float(_tkinter.TK_VERSION)
  61. TclVersion = float(_tkinter.TCL_VERSION)
  62. READABLE = _tkinter.READABLE
  63. WRITABLE = _tkinter.WRITABLE
  64. EXCEPTION = _tkinter.EXCEPTION
  65.  
  66. try:
  67.     _tkinter.createfilehandler
  68. except AttributeError:
  69.     _tkinter.createfilehandler = None
  70.  
  71.  
  72. try:
  73.     _tkinter.deletefilehandler
  74. except AttributeError:
  75.     _tkinter.deletefilehandler = None
  76.  
  77.  
  78. def _flatten(tuple):
  79.     '''Internal function.'''
  80.     res = ()
  81.     for item in tuple:
  82.         if type(item) in (TupleType, ListType):
  83.             res = res + _flatten(item)
  84.             continue
  85.         if item is not None:
  86.             res = res + (item,)
  87.             continue
  88.     
  89.     return res
  90.  
  91.  
  92. try:
  93.     _flatten = _tkinter._flatten
  94. except AttributeError:
  95.     pass
  96.  
  97.  
  98. def _cnfmerge(cnfs):
  99.     '''Internal function.'''
  100.     if type(cnfs) is DictionaryType:
  101.         return cnfs
  102.     elif type(cnfs) in (NoneType, StringType):
  103.         return cnfs
  104.     else:
  105.         cnf = { }
  106.         for c in _flatten(cnfs):
  107.             
  108.             try:
  109.                 cnf.update(c)
  110.             continue
  111.             except (AttributeError, TypeError):
  112.                 msg = None
  113.                 print '_cnfmerge: fallback due to:', msg
  114.                 for k, v in c.items():
  115.                     cnf[k] = v
  116.                 
  117.             
  118.  
  119.         
  120.         return cnf
  121.  
  122.  
  123. try:
  124.     _cnfmerge = _tkinter._cnfmerge
  125. except AttributeError:
  126.     pass
  127.  
  128.  
  129. class Event:
  130.     '''Container for the properties of an event.
  131.  
  132.     Instances of this type are generated if one of the following events occurs:
  133.  
  134.     KeyPress, KeyRelease - for keyboard events
  135.     ButtonPress, ButtonRelease, Motion, Enter, Leave, MouseWheel - for mouse events
  136.     Visibility, Unmap, Map, Expose, FocusIn, FocusOut, Circulate,
  137.     Colormap, Gravity, Reparent, Property, Destroy, Activate,
  138.     Deactivate - for window events.
  139.  
  140.     If a callback function for one of these events is registered
  141.     using bind, bind_all, bind_class, or tag_bind, the callback is
  142.     called with an Event as first argument. It will have the
  143.     following attributes (in braces are the event types for which
  144.     the attribute is valid):
  145.  
  146.         serial - serial number of event
  147.     num - mouse button pressed (ButtonPress, ButtonRelease)
  148.     focus - whether the window has the focus (Enter, Leave)
  149.     height - height of the exposed window (Configure, Expose)
  150.     width - width of the exposed window (Configure, Expose)
  151.     keycode - keycode of the pressed key (KeyPress, KeyRelease)
  152.     state - state of the event as a number (ButtonPress, ButtonRelease,
  153.                             Enter, KeyPress, KeyRelease,
  154.                             Leave, Motion)
  155.     state - state as a string (Visibility)
  156.     time - when the event occurred
  157.     x - x-position of the mouse
  158.     y - y-position of the mouse
  159.     x_root - x-position of the mouse on the screen
  160.              (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)
  161.     y_root - y-position of the mouse on the screen
  162.              (ButtonPress, ButtonRelease, KeyPress, KeyRelease, Motion)
  163.     char - pressed character (KeyPress, KeyRelease)
  164.     send_event - see X/Windows documentation
  165.     keysym - keysym of the the event as a string (KeyPress, KeyRelease)
  166.     keysym_num - keysym of the event as a number (KeyPress, KeyRelease)
  167.     type - type of the event as a number
  168.     widget - widget in which the event occurred
  169.     delta - delta of wheel movement (MouseWheel)
  170.     '''
  171.     pass
  172.  
  173. _support_default_root = 1
  174. _default_root = None
  175.  
  176. def NoDefaultRoot():
  177.     '''Inhibit setting of default root window.
  178.  
  179.     Call this function to inhibit that the first instance of
  180.     Tk is used for windows without an explicit parent window.
  181.     '''
  182.     global _support_default_root, _default_root, _default_root
  183.     _support_default_root = 0
  184.     _default_root = None
  185.     del _default_root
  186.  
  187.  
  188. def _tkerror(err):
  189.     '''Internal function.'''
  190.     pass
  191.  
  192.  
  193. def _exit(code = '0'):
  194.     '''Internal function. Calling it will throw the exception SystemExit.'''
  195.     raise SystemExit, code
  196.  
  197. _varnum = 0
  198.  
  199. class Variable:
  200.     '''Class to define value holders for e.g. buttons.
  201.  
  202.     Subclasses StringVar, IntVar, DoubleVar, BooleanVar are specializations
  203.     that constrain the type of the value returned from get().'''
  204.     _default = ''
  205.     
  206.     def __init__(self, master = None):
  207.         '''Construct a variable with an optional MASTER as master widget.
  208.         The variable is named PY_VAR_number in Tcl.
  209.         '''
  210.         global _varnum
  211.         if not master:
  212.             master = _default_root
  213.         
  214.         self._master = master
  215.         self._tk = master.tk
  216.         self._name = 'PY_VAR' + repr(_varnum)
  217.         _varnum = _varnum + 1
  218.         self.set(self._default)
  219.  
  220.     
  221.     def __del__(self):
  222.         '''Unset the variable in Tcl.'''
  223.         self._tk.globalunsetvar(self._name)
  224.  
  225.     
  226.     def __str__(self):
  227.         '''Return the name of the variable in Tcl.'''
  228.         return self._name
  229.  
  230.     
  231.     def set(self, value):
  232.         '''Set the variable to VALUE.'''
  233.         return self._tk.globalsetvar(self._name, value)
  234.  
  235.     
  236.     def get(self):
  237.         '''Return value of variable.'''
  238.         return self._tk.globalgetvar(self._name)
  239.  
  240.     
  241.     def trace_variable(self, mode, callback):
  242.         '''Define a trace callback for the variable.
  243.  
  244.         MODE is one of "r", "w", "u" for read, write, undefine.
  245.         CALLBACK must be a function which is called when
  246.         the variable is read, written or undefined.
  247.  
  248.         Return the name of the callback.
  249.         '''
  250.         cbname = self._master._register(callback)
  251.         self._tk.call('trace', 'variable', self._name, mode, cbname)
  252.         return cbname
  253.  
  254.     trace = trace_variable
  255.     
  256.     def trace_vdelete(self, mode, cbname):
  257.         '''Delete the trace callback for a variable.
  258.  
  259.         MODE is one of "r", "w", "u" for read, write, undefine.
  260.         CBNAME is the name of the callback returned from trace_variable or trace.
  261.         '''
  262.         self._tk.call('trace', 'vdelete', self._name, mode, cbname)
  263.         self._master.deletecommand(cbname)
  264.  
  265.     
  266.     def trace_vinfo(self):
  267.         '''Return all trace callback information.'''
  268.         return map(self._tk.split, self._tk.splitlist(self._tk.call('trace', 'vinfo', self._name)))
  269.  
  270.  
  271.  
  272. class StringVar(Variable):
  273.     '''Value holder for strings variables.'''
  274.     _default = ''
  275.     
  276.     def __init__(self, master = None):
  277.         '''Construct a string variable.
  278.  
  279.         MASTER can be given as master widget.'''
  280.         Variable.__init__(self, master)
  281.  
  282.     
  283.     def get(self):
  284.         '''Return value of variable as string.'''
  285.         value = self._tk.globalgetvar(self._name)
  286.         if isinstance(value, basestring):
  287.             return value
  288.         
  289.         return str(value)
  290.  
  291.  
  292.  
  293. class IntVar(Variable):
  294.     '''Value holder for integer variables.'''
  295.     _default = 0
  296.     
  297.     def __init__(self, master = None):
  298.         '''Construct an integer variable.
  299.  
  300.         MASTER can be given as master widget.'''
  301.         Variable.__init__(self, master)
  302.  
  303.     
  304.     def set(self, value):
  305.         '''Set the variable to value, converting booleans to integers.'''
  306.         if isinstance(value, bool):
  307.             value = int(value)
  308.         
  309.         return Variable.set(self, value)
  310.  
  311.     
  312.     def get(self):
  313.         '''Return the value of the variable as an integer.'''
  314.         return getint(self._tk.globalgetvar(self._name))
  315.  
  316.  
  317.  
  318. class DoubleVar(Variable):
  319.     '''Value holder for float variables.'''
  320.     _default = 0.0
  321.     
  322.     def __init__(self, master = None):
  323.         '''Construct a float variable.
  324.  
  325.         MASTER can be given as a master widget.'''
  326.         Variable.__init__(self, master)
  327.  
  328.     
  329.     def get(self):
  330.         '''Return the value of the variable as a float.'''
  331.         return getdouble(self._tk.globalgetvar(self._name))
  332.  
  333.  
  334.  
  335. class BooleanVar(Variable):
  336.     '''Value holder for boolean variables.'''
  337.     _default = 'false'
  338.     
  339.     def __init__(self, master = None):
  340.         '''Construct a boolean variable.
  341.  
  342.         MASTER can be given as a master widget.'''
  343.         Variable.__init__(self, master)
  344.  
  345.     
  346.     def get(self):
  347.         '''Return the value of the variable as a bool.'''
  348.         return self._tk.getboolean(self._tk.globalgetvar(self._name))
  349.  
  350.  
  351.  
  352. def mainloop(n = 0):
  353.     '''Run the main loop of Tcl.'''
  354.     _default_root.tk.mainloop(n)
  355.  
  356. getint = int
  357. getdouble = float
  358.  
  359. def getboolean(s):
  360.     '''Convert true and false to integer values 1 and 0.'''
  361.     return _default_root.tk.getboolean(s)
  362.  
  363.  
  364. class Misc:
  365.     '''Internal class.
  366.  
  367.     Base class which defines methods common for interior widgets.'''
  368.     _tclCommands = None
  369.     
  370.     def destroy(self):
  371.         '''Internal function.
  372.  
  373.         Delete all Tcl commands created for
  374.         this widget in the Tcl interpreter.'''
  375.         if self._tclCommands is not None:
  376.             for name in self._tclCommands:
  377.                 self.tk.deletecommand(name)
  378.             
  379.             self._tclCommands = None
  380.         
  381.  
  382.     
  383.     def deletecommand(self, name):
  384.         '''Internal function.
  385.  
  386.         Delete the Tcl command provided in NAME.'''
  387.         self.tk.deletecommand(name)
  388.         
  389.         try:
  390.             self._tclCommands.remove(name)
  391.         except ValueError:
  392.             pass
  393.  
  394.  
  395.     
  396.     def tk_strictMotif(self, boolean = None):
  397.         '''Set Tcl internal variable, whether the look and feel
  398.         should adhere to Motif.
  399.  
  400.         A parameter of 1 means adhere to Motif (e.g. no color
  401.         change if mouse passes over slider).
  402.         Returns the set value.'''
  403.         return self.tk.getboolean(self.tk.call('set', 'tk_strictMotif', boolean))
  404.  
  405.     
  406.     def tk_bisque(self):
  407.         '''Change the color scheme to light brown as used in Tk 3.6 and before.'''
  408.         self.tk.call('tk_bisque')
  409.  
  410.     
  411.     def tk_setPalette(self, *args, **kw):
  412.         '''Set a new color scheme for all widget elements.
  413.  
  414.         A single color as argument will cause that all colors of Tk
  415.         widget elements are derived from this.
  416.         Alternatively several keyword parameters and its associated
  417.         colors can be given. The following keywords are valid:
  418.         activeBackground, foreground, selectColor,
  419.         activeForeground, highlightBackground, selectBackground,
  420.         background, highlightColor, selectForeground,
  421.         disabledForeground, insertBackground, troughColor.'''
  422.         self.tk.call(('tk_setPalette',) + _flatten(args) + _flatten(kw.items()))
  423.  
  424.     
  425.     def tk_menuBar(self, *args):
  426.         '''Do not use. Needed in Tk 3.6 and earlier.'''
  427.         pass
  428.  
  429.     
  430.     def wait_variable(self, name = 'PY_VAR'):
  431.         '''Wait until the variable is modified.
  432.  
  433.         A parameter of type IntVar, StringVar, DoubleVar or
  434.         BooleanVar must be given.'''
  435.         self.tk.call('tkwait', 'variable', name)
  436.  
  437.     waitvar = wait_variable
  438.     
  439.     def wait_window(self, window = None):
  440.         '''Wait until a WIDGET is destroyed.
  441.  
  442.         If no parameter is given self is used.'''
  443.         if window is None:
  444.             window = self
  445.         
  446.         self.tk.call('tkwait', 'window', window._w)
  447.  
  448.     
  449.     def wait_visibility(self, window = None):
  450.         '''Wait until the visibility of a WIDGET changes
  451.         (e.g. it appears).
  452.  
  453.         If no parameter is given self is used.'''
  454.         if window is None:
  455.             window = self
  456.         
  457.         self.tk.call('tkwait', 'visibility', window._w)
  458.  
  459.     
  460.     def setvar(self, name = 'PY_VAR', value = '1'):
  461.         '''Set Tcl variable NAME to VALUE.'''
  462.         self.tk.setvar(name, value)
  463.  
  464.     
  465.     def getvar(self, name = 'PY_VAR'):
  466.         '''Return value of Tcl variable NAME.'''
  467.         return self.tk.getvar(name)
  468.  
  469.     getint = int
  470.     getdouble = float
  471.     
  472.     def getboolean(self, s):
  473.         '''Return a boolean value for Tcl boolean values true and false given as parameter.'''
  474.         return self.tk.getboolean(s)
  475.  
  476.     
  477.     def focus_set(self):
  478.         '''Direct input focus to this widget.
  479.  
  480.         If the application currently does not have the focus
  481.         this widget will get the focus if the application gets
  482.         the focus through the window manager.'''
  483.         self.tk.call('focus', self._w)
  484.  
  485.     focus = focus_set
  486.     
  487.     def focus_force(self):
  488.         '''Direct input focus to this widget even if the
  489.         application does not have the focus. Use with
  490.         caution!'''
  491.         self.tk.call('focus', '-force', self._w)
  492.  
  493.     
  494.     def focus_get(self):
  495.         '''Return the widget which has currently the focus in the
  496.         application.
  497.  
  498.         Use focus_displayof to allow working with several
  499.         displays. Return None if application does not have
  500.         the focus.'''
  501.         name = self.tk.call('focus')
  502.         if name == 'none' or not name:
  503.             return None
  504.         
  505.         return self._nametowidget(name)
  506.  
  507.     
  508.     def focus_displayof(self):
  509.         '''Return the widget which has currently the focus on the
  510.         display where this widget is located.
  511.  
  512.         Return None if the application does not have the focus.'''
  513.         name = self.tk.call('focus', '-displayof', self._w)
  514.         if name == 'none' or not name:
  515.             return None
  516.         
  517.         return self._nametowidget(name)
  518.  
  519.     
  520.     def focus_lastfor(self):
  521.         '''Return the widget which would have the focus if top level
  522.         for this widget gets the focus from the window manager.'''
  523.         name = self.tk.call('focus', '-lastfor', self._w)
  524.         if name == 'none' or not name:
  525.             return None
  526.         
  527.         return self._nametowidget(name)
  528.  
  529.     
  530.     def tk_focusFollowsMouse(self):
  531.         '''The widget under mouse will get automatically focus. Can not
  532.         be disabled easily.'''
  533.         self.tk.call('tk_focusFollowsMouse')
  534.  
  535.     
  536.     def tk_focusNext(self):
  537.         '''Return the next widget in the focus order which follows
  538.         widget which has currently the focus.
  539.  
  540.         The focus order first goes to the next child, then to
  541.         the children of the child recursively and then to the
  542.         next sibling which is higher in the stacking order.  A
  543.         widget is omitted if it has the takefocus resource set
  544.         to 0.'''
  545.         name = self.tk.call('tk_focusNext', self._w)
  546.         if not name:
  547.             return None
  548.         
  549.         return self._nametowidget(name)
  550.  
  551.     
  552.     def tk_focusPrev(self):
  553.         '''Return previous widget in the focus order. See tk_focusNext for details.'''
  554.         name = self.tk.call('tk_focusPrev', self._w)
  555.         if not name:
  556.             return None
  557.         
  558.         return self._nametowidget(name)
  559.  
  560.     
  561.     def after(self, ms, func = None, *args):
  562.         '''Call function once after given time.
  563.  
  564.         MS specifies the time in milliseconds. FUNC gives the
  565.         function which shall be called. Additional parameters
  566.         are given as parameters to the function call.  Return
  567.         identifier to cancel scheduling with after_cancel.'''
  568.         if not func:
  569.             self.tk.call('after', ms)
  570.         else:
  571.             tmp = []
  572.             
  573.             def callit(func = func, args = args, self = self, tmp = tmp):
  574.                 
  575.                 try:
  576.                     func(*args)
  577.                 finally:
  578.                     
  579.                     try:
  580.                         self.deletecommand(tmp[0])
  581.                     except TclError:
  582.                         pass
  583.  
  584.  
  585.  
  586.             name = self._register(callit)
  587.             tmp.append(name)
  588.             return self.tk.call('after', ms, name)
  589.  
  590.     
  591.     def after_idle(self, func, *args):
  592.         '''Call FUNC once if the Tcl main loop has no event to
  593.         process.
  594.  
  595.         Return an identifier to cancel the scheduling with
  596.         after_cancel.'''
  597.         return self.after('idle', func, *args)
  598.  
  599.     
  600.     def after_cancel(self, id):
  601.         '''Cancel scheduling of function identified with ID.
  602.  
  603.         Identifier returned by after or after_idle must be
  604.         given as first parameter.'''
  605.         
  606.         try:
  607.             data = self.tk.call('after', 'info', id)
  608.             script = self.tk.splitlist(data)[0]
  609.             self.deletecommand(script)
  610.         except TclError:
  611.             pass
  612.  
  613.         self.tk.call('after', 'cancel', id)
  614.  
  615.     
  616.     def bell(self, displayof = 0):
  617.         """Ring a display's bell."""
  618.         self.tk.call(('bell',) + self._displayof(displayof))
  619.  
  620.     
  621.     def clipboard_clear(self, **kw):
  622.         '''Clear the data in the Tk clipboard.
  623.  
  624.         A widget specified for the optional displayof keyword
  625.         argument specifies the target display.'''
  626.         if not kw.has_key('displayof'):
  627.             kw['displayof'] = self._w
  628.         
  629.         self.tk.call(('clipboard', 'clear') + self._options(kw))
  630.  
  631.     
  632.     def clipboard_append(self, string, **kw):
  633.         '''Append STRING to the Tk clipboard.
  634.  
  635.         A widget specified at the optional displayof keyword
  636.         argument specifies the target display. The clipboard
  637.         can be retrieved with selection_get.'''
  638.         if not kw.has_key('displayof'):
  639.             kw['displayof'] = self._w
  640.         
  641.         self.tk.call(('clipboard', 'append') + self._options(kw) + ('--', string))
  642.  
  643.     
  644.     def grab_current(self):
  645.         '''Return widget which has currently the grab in this application
  646.         or None.'''
  647.         name = self.tk.call('grab', 'current', self._w)
  648.         if not name:
  649.             return None
  650.         
  651.         return self._nametowidget(name)
  652.  
  653.     
  654.     def grab_release(self):
  655.         '''Release grab for this widget if currently set.'''
  656.         self.tk.call('grab', 'release', self._w)
  657.  
  658.     
  659.     def grab_set(self):
  660.         '''Set grab for this widget.
  661.  
  662.         A grab directs all events to this and descendant
  663.         widgets in the application.'''
  664.         self.tk.call('grab', 'set', self._w)
  665.  
  666.     
  667.     def grab_set_global(self):
  668.         '''Set global grab for this widget.
  669.  
  670.         A global grab directs all events to this and
  671.         descendant widgets on the display. Use with caution -
  672.         other applications do not get events anymore.'''
  673.         self.tk.call('grab', 'set', '-global', self._w)
  674.  
  675.     
  676.     def grab_status(self):
  677.         '''Return None, "local" or "global" if this widget has
  678.         no, a local or a global grab.'''
  679.         status = self.tk.call('grab', 'status', self._w)
  680.         if status == 'none':
  681.             status = None
  682.         
  683.         return status
  684.  
  685.     
  686.     def lower(self, belowThis = None):
  687.         '''Lower this widget in the stacking order.'''
  688.         self.tk.call('lower', self._w, belowThis)
  689.  
  690.     
  691.     def option_add(self, pattern, value, priority = None):
  692.         '''Set a VALUE (second parameter) for an option
  693.         PATTERN (first parameter).
  694.  
  695.         An optional third parameter gives the numeric priority
  696.         (defaults to 80).'''
  697.         self.tk.call('option', 'add', pattern, value, priority)
  698.  
  699.     
  700.     def option_clear(self):
  701.         '''Clear the option database.
  702.  
  703.         It will be reloaded if option_add is called.'''
  704.         self.tk.call('option', 'clear')
  705.  
  706.     
  707.     def option_get(self, name, className):
  708.         '''Return the value for an option NAME for this widget
  709.         with CLASSNAME.
  710.  
  711.         Values with higher priority override lower values.'''
  712.         return self.tk.call('option', 'get', self._w, name, className)
  713.  
  714.     
  715.     def option_readfile(self, fileName, priority = None):
  716.         '''Read file FILENAME into the option database.
  717.  
  718.         An optional second parameter gives the numeric
  719.         priority.'''
  720.         self.tk.call('option', 'readfile', fileName, priority)
  721.  
  722.     
  723.     def selection_clear(self, **kw):
  724.         '''Clear the current X selection.'''
  725.         if not kw.has_key('displayof'):
  726.             kw['displayof'] = self._w
  727.         
  728.         self.tk.call(('selection', 'clear') + self._options(kw))
  729.  
  730.     
  731.     def selection_get(self, **kw):
  732.         '''Return the contents of the current X selection.
  733.  
  734.         A keyword parameter selection specifies the name of
  735.         the selection and defaults to PRIMARY.  A keyword
  736.         parameter displayof specifies a widget on the display
  737.         to use.'''
  738.         if not kw.has_key('displayof'):
  739.             kw['displayof'] = self._w
  740.         
  741.         return self.tk.call(('selection', 'get') + self._options(kw))
  742.  
  743.     
  744.     def selection_handle(self, command, **kw):
  745.         '''Specify a function COMMAND to call if the X
  746.         selection owned by this widget is queried by another
  747.         application.
  748.  
  749.         This function must return the contents of the
  750.         selection. The function will be called with the
  751.         arguments OFFSET and LENGTH which allows the chunking
  752.         of very long selections. The following keyword
  753.         parameters can be provided:
  754.         selection - name of the selection (default PRIMARY),
  755.         type - type of the selection (e.g. STRING, FILE_NAME).'''
  756.         name = self._register(command)
  757.         self.tk.call(('selection', 'handle') + self._options(kw) + (self._w, name))
  758.  
  759.     
  760.     def selection_own(self, **kw):
  761.         '''Become owner of X selection.
  762.  
  763.         A keyword parameter selection specifies the name of
  764.         the selection (default PRIMARY).'''
  765.         self.tk.call(('selection', 'own') + self._options(kw) + (self._w,))
  766.  
  767.     
  768.     def selection_own_get(self, **kw):
  769.         '''Return owner of X selection.
  770.  
  771.         The following keyword parameter can
  772.         be provided:
  773.         selection - name of the selection (default PRIMARY),
  774.         type - type of the selection (e.g. STRING, FILE_NAME).'''
  775.         if not kw.has_key('displayof'):
  776.             kw['displayof'] = self._w
  777.         
  778.         name = self.tk.call(('selection', 'own') + self._options(kw))
  779.         if not name:
  780.             return None
  781.         
  782.         return self._nametowidget(name)
  783.  
  784.     
  785.     def send(self, interp, cmd, *args):
  786.         '''Send Tcl command CMD to different interpreter INTERP to be executed.'''
  787.         return self.tk.call(('send', interp, cmd) + args)
  788.  
  789.     
  790.     def lower(self, belowThis = None):
  791.         '''Lower this widget in the stacking order.'''
  792.         self.tk.call('lower', self._w, belowThis)
  793.  
  794.     
  795.     def tkraise(self, aboveThis = None):
  796.         '''Raise this widget in the stacking order.'''
  797.         self.tk.call('raise', self._w, aboveThis)
  798.  
  799.     lift = tkraise
  800.     
  801.     def colormodel(self, value = None):
  802.         '''Useless. Not implemented in Tk.'''
  803.         return self.tk.call('tk', 'colormodel', self._w, value)
  804.  
  805.     
  806.     def winfo_atom(self, name, displayof = 0):
  807.         '''Return integer which represents atom NAME.'''
  808.         args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
  809.         return getint(self.tk.call(args))
  810.  
  811.     
  812.     def winfo_atomname(self, id, displayof = 0):
  813.         '''Return name of atom with identifier ID.'''
  814.         args = ('winfo', 'atomname') + self._displayof(displayof) + (id,)
  815.         return self.tk.call(args)
  816.  
  817.     
  818.     def winfo_cells(self):
  819.         '''Return number of cells in the colormap for this widget.'''
  820.         return getint(self.tk.call('winfo', 'cells', self._w))
  821.  
  822.     
  823.     def winfo_children(self):
  824.         '''Return a list of all widgets which are children of this widget.'''
  825.         result = []
  826.         for child in self.tk.splitlist(self.tk.call('winfo', 'children', self._w)):
  827.             
  828.             try:
  829.                 result.append(self._nametowidget(child))
  830.             continue
  831.             except KeyError:
  832.                 continue
  833.             
  834.  
  835.         
  836.         return result
  837.  
  838.     
  839.     def winfo_class(self):
  840.         '''Return window class name of this widget.'''
  841.         return self.tk.call('winfo', 'class', self._w)
  842.  
  843.     
  844.     def winfo_colormapfull(self):
  845.         '''Return true if at the last color request the colormap was full.'''
  846.         return self.tk.getboolean(self.tk.call('winfo', 'colormapfull', self._w))
  847.  
  848.     
  849.     def winfo_containing(self, rootX, rootY, displayof = 0):
  850.         '''Return the widget which is at the root coordinates ROOTX, ROOTY.'''
  851.         args = ('winfo', 'containing') + self._displayof(displayof) + (rootX, rootY)
  852.         name = self.tk.call(args)
  853.         if not name:
  854.             return None
  855.         
  856.         return self._nametowidget(name)
  857.  
  858.     
  859.     def winfo_depth(self):
  860.         '''Return the number of bits per pixel.'''
  861.         return getint(self.tk.call('winfo', 'depth', self._w))
  862.  
  863.     
  864.     def winfo_exists(self):
  865.         '''Return true if this widget exists.'''
  866.         return getint(self.tk.call('winfo', 'exists', self._w))
  867.  
  868.     
  869.     def winfo_fpixels(self, number):
  870.         '''Return the number of pixels for the given distance NUMBER
  871.         (e.g. "3c") as float.'''
  872.         return getdouble(self.tk.call('winfo', 'fpixels', self._w, number))
  873.  
  874.     
  875.     def winfo_geometry(self):
  876.         '''Return geometry string for this widget in the form "widthxheight+X+Y".'''
  877.         return self.tk.call('winfo', 'geometry', self._w)
  878.  
  879.     
  880.     def winfo_height(self):
  881.         '''Return height of this widget.'''
  882.         return getint(self.tk.call('winfo', 'height', self._w))
  883.  
  884.     
  885.     def winfo_id(self):
  886.         '''Return identifier ID for this widget.'''
  887.         return self.tk.getint(self.tk.call('winfo', 'id', self._w))
  888.  
  889.     
  890.     def winfo_interps(self, displayof = 0):
  891.         '''Return the name of all Tcl interpreters for this display.'''
  892.         args = ('winfo', 'interps') + self._displayof(displayof)
  893.         return self.tk.splitlist(self.tk.call(args))
  894.  
  895.     
  896.     def winfo_ismapped(self):
  897.         '''Return true if this widget is mapped.'''
  898.         return getint(self.tk.call('winfo', 'ismapped', self._w))
  899.  
  900.     
  901.     def winfo_manager(self):
  902.         '''Return the window mananger name for this widget.'''
  903.         return self.tk.call('winfo', 'manager', self._w)
  904.  
  905.     
  906.     def winfo_name(self):
  907.         '''Return the name of this widget.'''
  908.         return self.tk.call('winfo', 'name', self._w)
  909.  
  910.     
  911.     def winfo_parent(self):
  912.         '''Return the name of the parent of this widget.'''
  913.         return self.tk.call('winfo', 'parent', self._w)
  914.  
  915.     
  916.     def winfo_pathname(self, id, displayof = 0):
  917.         '''Return the pathname of the widget given by ID.'''
  918.         args = ('winfo', 'pathname') + self._displayof(displayof) + (id,)
  919.         return self.tk.call(args)
  920.  
  921.     
  922.     def winfo_pixels(self, number):
  923.         '''Rounded integer value of winfo_fpixels.'''
  924.         return getint(self.tk.call('winfo', 'pixels', self._w, number))
  925.  
  926.     
  927.     def winfo_pointerx(self):
  928.         '''Return the x coordinate of the pointer on the root window.'''
  929.         return getint(self.tk.call('winfo', 'pointerx', self._w))
  930.  
  931.     
  932.     def winfo_pointerxy(self):
  933.         '''Return a tuple of x and y coordinates of the pointer on the root window.'''
  934.         return self._getints(self.tk.call('winfo', 'pointerxy', self._w))
  935.  
  936.     
  937.     def winfo_pointery(self):
  938.         '''Return the y coordinate of the pointer on the root window.'''
  939.         return getint(self.tk.call('winfo', 'pointery', self._w))
  940.  
  941.     
  942.     def winfo_reqheight(self):
  943.         '''Return requested height of this widget.'''
  944.         return getint(self.tk.call('winfo', 'reqheight', self._w))
  945.  
  946.     
  947.     def winfo_reqwidth(self):
  948.         '''Return requested width of this widget.'''
  949.         return getint(self.tk.call('winfo', 'reqwidth', self._w))
  950.  
  951.     
  952.     def winfo_rgb(self, color):
  953.         '''Return tuple of decimal values for red, green, blue for
  954.         COLOR in this widget.'''
  955.         return self._getints(self.tk.call('winfo', 'rgb', self._w, color))
  956.  
  957.     
  958.     def winfo_rootx(self):
  959.         '''Return x coordinate of upper left corner of this widget on the
  960.         root window.'''
  961.         return getint(self.tk.call('winfo', 'rootx', self._w))
  962.  
  963.     
  964.     def winfo_rooty(self):
  965.         '''Return y coordinate of upper left corner of this widget on the
  966.         root window.'''
  967.         return getint(self.tk.call('winfo', 'rooty', self._w))
  968.  
  969.     
  970.     def winfo_screen(self):
  971.         '''Return the screen name of this widget.'''
  972.         return self.tk.call('winfo', 'screen', self._w)
  973.  
  974.     
  975.     def winfo_screencells(self):
  976.         '''Return the number of the cells in the colormap of the screen
  977.         of this widget.'''
  978.         return getint(self.tk.call('winfo', 'screencells', self._w))
  979.  
  980.     
  981.     def winfo_screendepth(self):
  982.         '''Return the number of bits per pixel of the root window of the
  983.         screen of this widget.'''
  984.         return getint(self.tk.call('winfo', 'screendepth', self._w))
  985.  
  986.     
  987.     def winfo_screenheight(self):
  988.         '''Return the number of pixels of the height of the screen of this widget
  989.         in pixel.'''
  990.         return getint(self.tk.call('winfo', 'screenheight', self._w))
  991.  
  992.     
  993.     def winfo_screenmmheight(self):
  994.         '''Return the number of pixels of the height of the screen of
  995.         this widget in mm.'''
  996.         return getint(self.tk.call('winfo', 'screenmmheight', self._w))
  997.  
  998.     
  999.     def winfo_screenmmwidth(self):
  1000.         '''Return the number of pixels of the width of the screen of
  1001.         this widget in mm.'''
  1002.         return getint(self.tk.call('winfo', 'screenmmwidth', self._w))
  1003.  
  1004.     
  1005.     def winfo_screenvisual(self):
  1006.         '''Return one of the strings directcolor, grayscale, pseudocolor,
  1007.         staticcolor, staticgray, or truecolor for the default
  1008.         colormodel of this screen.'''
  1009.         return self.tk.call('winfo', 'screenvisual', self._w)
  1010.  
  1011.     
  1012.     def winfo_screenwidth(self):
  1013.         '''Return the number of pixels of the width of the screen of
  1014.         this widget in pixel.'''
  1015.         return getint(self.tk.call('winfo', 'screenwidth', self._w))
  1016.  
  1017.     
  1018.     def winfo_server(self):
  1019.         '''Return information of the X-Server of the screen of this widget in
  1020.         the form "XmajorRminor vendor vendorVersion".'''
  1021.         return self.tk.call('winfo', 'server', self._w)
  1022.  
  1023.     
  1024.     def winfo_toplevel(self):
  1025.         '''Return the toplevel widget of this widget.'''
  1026.         return self._nametowidget(self.tk.call('winfo', 'toplevel', self._w))
  1027.  
  1028.     
  1029.     def winfo_viewable(self):
  1030.         '''Return true if the widget and all its higher ancestors are mapped.'''
  1031.         return getint(self.tk.call('winfo', 'viewable', self._w))
  1032.  
  1033.     
  1034.     def winfo_visual(self):
  1035.         '''Return one of the strings directcolor, grayscale, pseudocolor,
  1036.         staticcolor, staticgray, or truecolor for the
  1037.         colormodel of this widget.'''
  1038.         return self.tk.call('winfo', 'visual', self._w)
  1039.  
  1040.     
  1041.     def winfo_visualid(self):
  1042.         '''Return the X identifier for the visual for this widget.'''
  1043.         return self.tk.call('winfo', 'visualid', self._w)
  1044.  
  1045.     
  1046.     def winfo_visualsavailable(self, includeids = 0):
  1047.         '''Return a list of all visuals available for the screen
  1048.         of this widget.
  1049.  
  1050.         Each item in the list consists of a visual name (see winfo_visual), a
  1051.         depth and if INCLUDEIDS=1 is given also the X identifier.'''
  1052.         if not includeids or 'includeids':
  1053.             pass
  1054.         data = self.tk.split(self.tk.call('winfo', 'visualsavailable', self._w, None))
  1055.         if type(data) is StringType:
  1056.             data = [
  1057.                 self.tk.split(data)]
  1058.         
  1059.         return map(self._Misc__winfo_parseitem, data)
  1060.  
  1061.     
  1062.     def __winfo_parseitem(self, t):
  1063.         '''Internal function.'''
  1064.         return t[:1] + tuple(map(self._Misc__winfo_getint, t[1:]))
  1065.  
  1066.     
  1067.     def __winfo_getint(self, x):
  1068.         '''Internal function.'''
  1069.         return int(x, 0)
  1070.  
  1071.     
  1072.     def winfo_vrootheight(self):
  1073.         '''Return the height of the virtual root window associated with this
  1074.         widget in pixels. If there is no virtual root window return the
  1075.         height of the screen.'''
  1076.         return getint(self.tk.call('winfo', 'vrootheight', self._w))
  1077.  
  1078.     
  1079.     def winfo_vrootwidth(self):
  1080.         '''Return the width of the virtual root window associated with this
  1081.         widget in pixel. If there is no virtual root window return the
  1082.         width of the screen.'''
  1083.         return getint(self.tk.call('winfo', 'vrootwidth', self._w))
  1084.  
  1085.     
  1086.     def winfo_vrootx(self):
  1087.         '''Return the x offset of the virtual root relative to the root
  1088.         window of the screen of this widget.'''
  1089.         return getint(self.tk.call('winfo', 'vrootx', self._w))
  1090.  
  1091.     
  1092.     def winfo_vrooty(self):
  1093.         '''Return the y offset of the virtual root relative to the root
  1094.         window of the screen of this widget.'''
  1095.         return getint(self.tk.call('winfo', 'vrooty', self._w))
  1096.  
  1097.     
  1098.     def winfo_width(self):
  1099.         '''Return the width of this widget.'''
  1100.         return getint(self.tk.call('winfo', 'width', self._w))
  1101.  
  1102.     
  1103.     def winfo_x(self):
  1104.         '''Return the x coordinate of the upper left corner of this widget
  1105.         in the parent.'''
  1106.         return getint(self.tk.call('winfo', 'x', self._w))
  1107.  
  1108.     
  1109.     def winfo_y(self):
  1110.         '''Return the y coordinate of the upper left corner of this widget
  1111.         in the parent.'''
  1112.         return getint(self.tk.call('winfo', 'y', self._w))
  1113.  
  1114.     
  1115.     def update(self):
  1116.         '''Enter event loop until all pending events have been processed by Tcl.'''
  1117.         self.tk.call('update')
  1118.  
  1119.     
  1120.     def update_idletasks(self):
  1121.         '''Enter event loop until all idle callbacks have been called. This
  1122.         will update the display of windows but not process events caused by
  1123.         the user.'''
  1124.         self.tk.call('update', 'idletasks')
  1125.  
  1126.     
  1127.     def bindtags(self, tagList = None):
  1128.         '''Set or get the list of bindtags for this widget.
  1129.  
  1130.         With no argument return the list of all bindtags associated with
  1131.         this widget. With a list of strings as argument the bindtags are
  1132.         set to this list. The bindtags determine in which order events are
  1133.         processed (see bind).'''
  1134.         if tagList is None:
  1135.             return self.tk.splitlist(self.tk.call('bindtags', self._w))
  1136.         else:
  1137.             self.tk.call('bindtags', self._w, tagList)
  1138.  
  1139.     
  1140.     def _bind(self, what, sequence, func, add, needcleanup = 1):
  1141.         '''Internal function.'''
  1142.         if type(func) is StringType:
  1143.             self.tk.call(what + (sequence, func))
  1144.         elif func:
  1145.             funcid = self._register(func, self._substitute, needcleanup)
  1146.             if not add or '+':
  1147.                 pass
  1148.             cmd = '%sif {"[%s %s]" == "break"} break\n' % ('', funcid, self._subst_format_str)
  1149.             self.tk.call(what + (sequence, cmd))
  1150.             return funcid
  1151.         elif sequence:
  1152.             return self.tk.call(what + (sequence,))
  1153.         else:
  1154.             return self.tk.splitlist(self.tk.call(what))
  1155.  
  1156.     
  1157.     def bind(self, sequence = None, func = None, add = None):
  1158.         '''Bind to this widget at event SEQUENCE a call to function FUNC.
  1159.  
  1160.         SEQUENCE is a string of concatenated event
  1161.         patterns. An event pattern is of the form
  1162.         <MODIFIER-MODIFIER-TYPE-DETAIL> where MODIFIER is one
  1163.         of Control, Mod2, M2, Shift, Mod3, M3, Lock, Mod4, M4,
  1164.         Button1, B1, Mod5, M5 Button2, B2, Meta, M, Button3,
  1165.         B3, Alt, Button4, B4, Double, Button5, B5 Triple,
  1166.         Mod1, M1. TYPE is one of Activate, Enter, Map,
  1167.         ButtonPress, Button, Expose, Motion, ButtonRelease
  1168.         FocusIn, MouseWheel, Circulate, FocusOut, Property,
  1169.         Colormap, Gravity Reparent, Configure, KeyPress, Key,
  1170.         Unmap, Deactivate, KeyRelease Visibility, Destroy,
  1171.         Leave and DETAIL is the button number for ButtonPress,
  1172.         ButtonRelease and DETAIL is the Keysym for KeyPress and
  1173.         KeyRelease. Examples are
  1174.         <Control-Button-1> for pressing Control and mouse button 1 or
  1175.         <Alt-A> for pressing A and the Alt key (KeyPress can be omitted).
  1176.         An event pattern can also be a virtual event of the form
  1177.         <<AString>> where AString can be arbitrary. This
  1178.         event can be generated by event_generate.
  1179.         If events are concatenated they must appear shortly
  1180.         after each other.
  1181.  
  1182.         FUNC will be called if the event sequence occurs with an
  1183.         instance of Event as argument. If the return value of FUNC is
  1184.         "break" no further bound function is invoked.
  1185.  
  1186.         An additional boolean parameter ADD specifies whether FUNC will
  1187.         be called additionally to the other bound function or whether
  1188.         it will replace the previous function.
  1189.  
  1190.         Bind will return an identifier to allow deletion of the bound function with
  1191.         unbind without memory leak.
  1192.  
  1193.         If FUNC or SEQUENCE is omitted the bound function or list
  1194.         of bound events are returned.'''
  1195.         return self._bind(('bind', self._w), sequence, func, add)
  1196.  
  1197.     
  1198.     def unbind(self, sequence, funcid = None):
  1199.         '''Unbind for this widget for event SEQUENCE  the
  1200.         function identified with FUNCID.'''
  1201.         self.tk.call('bind', self._w, sequence, '')
  1202.         if funcid:
  1203.             self.deletecommand(funcid)
  1204.         
  1205.  
  1206.     
  1207.     def bind_all(self, sequence = None, func = None, add = None):
  1208.         '''Bind to all widgets at an event SEQUENCE a call to function FUNC.
  1209.         An additional boolean parameter ADD specifies whether FUNC will
  1210.         be called additionally to the other bound function or whether
  1211.         it will replace the previous function. See bind for the return value.'''
  1212.         return self._bind(('bind', 'all'), sequence, func, add, 0)
  1213.  
  1214.     
  1215.     def unbind_all(self, sequence):
  1216.         '''Unbind for all widgets for event SEQUENCE all functions.'''
  1217.         self.tk.call('bind', 'all', sequence, '')
  1218.  
  1219.     
  1220.     def bind_class(self, className, sequence = None, func = None, add = None):
  1221.         '''Bind to widgets with bindtag CLASSNAME at event
  1222.         SEQUENCE a call of function FUNC. An additional
  1223.         boolean parameter ADD specifies whether FUNC will be
  1224.         called additionally to the other bound function or
  1225.         whether it will replace the previous function. See bind for
  1226.         the return value.'''
  1227.         return self._bind(('bind', className), sequence, func, add, 0)
  1228.  
  1229.     
  1230.     def unbind_class(self, className, sequence):
  1231.         '''Unbind for a all widgets with bindtag CLASSNAME for event SEQUENCE
  1232.         all functions.'''
  1233.         self.tk.call('bind', className, sequence, '')
  1234.  
  1235.     
  1236.     def mainloop(self, n = 0):
  1237.         '''Call the mainloop of Tk.'''
  1238.         self.tk.mainloop(n)
  1239.  
  1240.     
  1241.     def quit(self):
  1242.         '''Quit the Tcl interpreter. All widgets will be destroyed.'''
  1243.         self.tk.quit()
  1244.  
  1245.     
  1246.     def _getints(self, string):
  1247.         '''Internal function.'''
  1248.         if string:
  1249.             return tuple(map(getint, self.tk.splitlist(string)))
  1250.         
  1251.  
  1252.     
  1253.     def _getdoubles(self, string):
  1254.         '''Internal function.'''
  1255.         if string:
  1256.             return tuple(map(getdouble, self.tk.splitlist(string)))
  1257.         
  1258.  
  1259.     
  1260.     def _getboolean(self, string):
  1261.         '''Internal function.'''
  1262.         if string:
  1263.             return self.tk.getboolean(string)
  1264.         
  1265.  
  1266.     
  1267.     def _displayof(self, displayof):
  1268.         '''Internal function.'''
  1269.         if displayof:
  1270.             return ('-displayof', displayof)
  1271.         
  1272.         if displayof is None:
  1273.             return ('-displayof', self._w)
  1274.         
  1275.         return ()
  1276.  
  1277.     
  1278.     def _options(self, cnf, kw = None):
  1279.         '''Internal function.'''
  1280.         if kw:
  1281.             cnf = _cnfmerge((cnf, kw))
  1282.         else:
  1283.             cnf = _cnfmerge(cnf)
  1284.         res = ()
  1285.         for k, v in cnf.items():
  1286.             if v is not None:
  1287.                 if k[-1] == '_':
  1288.                     k = k[:-1]
  1289.                 
  1290.                 if callable(v):
  1291.                     v = self._register(v)
  1292.                 
  1293.                 res = res + ('-' + k, v)
  1294.                 continue
  1295.         
  1296.         return res
  1297.  
  1298.     
  1299.     def nametowidget(self, name):
  1300.         '''Return the Tkinter instance of a widget identified by
  1301.         its Tcl name NAME.'''
  1302.         w = self
  1303.         if name[0] == '.':
  1304.             w = w._root()
  1305.             name = name[1:]
  1306.         
  1307.         while name:
  1308.             i = name.find('.')
  1309.             if i >= 0:
  1310.                 name = name[:i]
  1311.                 tail = name[i + 1:]
  1312.             else:
  1313.                 tail = ''
  1314.             w = w.children[name]
  1315.             name = tail
  1316.         return w
  1317.  
  1318.     _nametowidget = nametowidget
  1319.     
  1320.     def _register(self, func, subst = None, needcleanup = 1):
  1321.         '''Return a newly created Tcl function. If this
  1322.         function is called, the Python function FUNC will
  1323.         be executed. An optional function SUBST can
  1324.         be given which will be executed before FUNC.'''
  1325.         f = CallWrapper(func, subst, self).__call__
  1326.         name = repr(id(f))
  1327.         
  1328.         try:
  1329.             func = func.im_func
  1330.         except AttributeError:
  1331.             pass
  1332.  
  1333.         
  1334.         try:
  1335.             name = name + func.__name__
  1336.         except AttributeError:
  1337.             pass
  1338.  
  1339.         self.tk.createcommand(name, f)
  1340.         if needcleanup:
  1341.             if self._tclCommands is None:
  1342.                 self._tclCommands = []
  1343.             
  1344.             self._tclCommands.append(name)
  1345.         
  1346.         return name
  1347.  
  1348.     register = _register
  1349.     
  1350.     def _root(self):
  1351.         '''Internal function.'''
  1352.         w = self
  1353.         while w.master:
  1354.             w = w.master
  1355.         return w
  1356.  
  1357.     _subst_format = ('%#', '%b', '%f', '%h', '%k', '%s', '%t', '%w', '%x', '%y', '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y', '%D')
  1358.     _subst_format_str = ' '.join(_subst_format)
  1359.     
  1360.     def _substitute(self, *args):
  1361.         '''Internal function.'''
  1362.         if len(args) != len(self._subst_format):
  1363.             return args
  1364.         
  1365.         getboolean = self.tk.getboolean
  1366.         getint = int
  1367.         
  1368.         def getint_event(s):
  1369.             '''Tk changed behavior in 8.4.2, returning "??" rather more often.'''
  1370.             
  1371.             try:
  1372.                 return int(s)
  1373.             except ValueError:
  1374.                 return s
  1375.  
  1376.  
  1377.         (nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y, D) = args
  1378.         e = Event()
  1379.         e.serial = getint(nsign)
  1380.         e.num = getint_event(b)
  1381.         
  1382.         try:
  1383.             e.focus = getboolean(f)
  1384.         except TclError:
  1385.             pass
  1386.  
  1387.         e.height = getint_event(h)
  1388.         e.keycode = getint_event(k)
  1389.         e.state = getint_event(s)
  1390.         e.time = getint_event(t)
  1391.         e.width = getint_event(w)
  1392.         e.x = getint_event(x)
  1393.         e.y = getint_event(y)
  1394.         e.char = A
  1395.         
  1396.         try:
  1397.             e.send_event = getboolean(E)
  1398.         except TclError:
  1399.             pass
  1400.  
  1401.         e.keysym = K
  1402.         e.keysym_num = getint_event(N)
  1403.         e.type = T
  1404.         
  1405.         try:
  1406.             e.widget = self._nametowidget(W)
  1407.         except KeyError:
  1408.             e.widget = W
  1409.  
  1410.         e.x_root = getint_event(X)
  1411.         e.y_root = getint_event(Y)
  1412.         
  1413.         try:
  1414.             e.delta = getint(D)
  1415.         except ValueError:
  1416.             e.delta = 0
  1417.  
  1418.         return (e,)
  1419.  
  1420.     
  1421.     def _report_exception(self):
  1422.         '''Internal function.'''
  1423.         import sys
  1424.         exc = sys.exc_type
  1425.         val = sys.exc_value
  1426.         tb = sys.exc_traceback
  1427.         root = self._root()
  1428.         root.report_callback_exception(exc, val, tb)
  1429.  
  1430.     
  1431.     def _configure(self, cmd, cnf, kw):
  1432.         '''Internal function.'''
  1433.         if kw:
  1434.             cnf = _cnfmerge((cnf, kw))
  1435.         elif cnf:
  1436.             cnf = _cnfmerge(cnf)
  1437.         
  1438.         if cnf is None:
  1439.             cnf = { }
  1440.             for x in self.tk.split(self.tk.call(_flatten((self._w, cmd)))):
  1441.                 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
  1442.             
  1443.             return cnf
  1444.         
  1445.         if type(cnf) is StringType:
  1446.             x = self.tk.split(self.tk.call(_flatten((self._w, cmd, '-' + cnf))))
  1447.             return (x[0][1:],) + x[1:]
  1448.         
  1449.         self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
  1450.  
  1451.     
  1452.     def configure(self, cnf = None, **kw):
  1453.         '''Configure resources of a widget.
  1454.  
  1455.         The values for resources are specified as keyword
  1456.         arguments. To get an overview about
  1457.         the allowed keyword arguments call the method keys.
  1458.         '''
  1459.         return self._configure('configure', cnf, kw)
  1460.  
  1461.     config = configure
  1462.     
  1463.     def cget(self, key):
  1464.         '''Return the resource value for a KEY given as string.'''
  1465.         return self.tk.call(self._w, 'cget', '-' + key)
  1466.  
  1467.     __getitem__ = cget
  1468.     
  1469.     def __setitem__(self, key, value):
  1470.         self.configure({
  1471.             key: value })
  1472.  
  1473.     
  1474.     def keys(self):
  1475.         '''Return a list of all resource names of this widget.'''
  1476.         return map((lambda x: x[0][1:]), self.tk.split(self.tk.call(self._w, 'configure')))
  1477.  
  1478.     
  1479.     def __str__(self):
  1480.         '''Return the window path name of this widget.'''
  1481.         return self._w
  1482.  
  1483.     _noarg_ = [
  1484.         '_noarg_']
  1485.     
  1486.     def pack_propagate(self, flag = _noarg_):
  1487.         '''Set or get the status for propagation of geometry information.
  1488.  
  1489.         A boolean argument specifies whether the geometry information
  1490.         of the slaves will determine the size of this widget. If no argument
  1491.         is given the current setting will be returned.
  1492.         '''
  1493.         if flag is Misc._noarg_:
  1494.             return self._getboolean(self.tk.call('pack', 'propagate', self._w))
  1495.         else:
  1496.             self.tk.call('pack', 'propagate', self._w, flag)
  1497.  
  1498.     propagate = pack_propagate
  1499.     
  1500.     def pack_slaves(self):
  1501.         '''Return a list of all slaves of this widget
  1502.         in its packing order.'''
  1503.         return map(self._nametowidget, self.tk.splitlist(self.tk.call('pack', 'slaves', self._w)))
  1504.  
  1505.     slaves = pack_slaves
  1506.     
  1507.     def place_slaves(self):
  1508.         '''Return a list of all slaves of this widget
  1509.         in its packing order.'''
  1510.         return map(self._nametowidget, self.tk.splitlist(self.tk.call('place', 'slaves', self._w)))
  1511.  
  1512.     
  1513.     def grid_bbox(self, column = None, row = None, col2 = None, row2 = None):
  1514.         '''Return a tuple of integer coordinates for the bounding
  1515.         box of this widget controlled by the geometry manager grid.
  1516.  
  1517.         If COLUMN, ROW is given the bounding box applies from
  1518.         the cell with row and column 0 to the specified
  1519.         cell. If COL2 and ROW2 are given the bounding box
  1520.         starts at that cell.
  1521.  
  1522.         The returned integers specify the offset of the upper left
  1523.         corner in the master widget and the width and height.
  1524.         '''
  1525.         args = ('grid', 'bbox', self._w)
  1526.         if column is not None and row is not None:
  1527.             args = args + (column, row)
  1528.         
  1529.         if col2 is not None and row2 is not None:
  1530.             args = args + (col2, row2)
  1531.         
  1532.         if not self._getints(self.tk.call(*args)):
  1533.             pass
  1534.  
  1535.     bbox = grid_bbox
  1536.     
  1537.     def _grid_configure(self, command, index, cnf, kw):
  1538.         '''Internal function.'''
  1539.         if type(cnf) is StringType and not kw:
  1540.             if cnf[-1:] == '_':
  1541.                 cnf = cnf[:-1]
  1542.             
  1543.             if cnf[:1] != '-':
  1544.                 cnf = '-' + cnf
  1545.             
  1546.             options = (cnf,)
  1547.         else:
  1548.             options = self._options(cnf, kw)
  1549.         if not options:
  1550.             res = self.tk.call('grid', command, self._w, index)
  1551.             words = self.tk.splitlist(res)
  1552.             dict = { }
  1553.             for i in range(0, len(words), 2):
  1554.                 key = words[i][1:]
  1555.                 value = words[i + 1]
  1556.                 if not value:
  1557.                     value = None
  1558.                 elif '.' in value:
  1559.                     value = getdouble(value)
  1560.                 else:
  1561.                     value = getint(value)
  1562.                 dict[key] = value
  1563.             
  1564.             return dict
  1565.         
  1566.         res = self.tk.call(('grid', command, self._w, index) + options)
  1567.         if len(options) == 1:
  1568.             if not res:
  1569.                 return None
  1570.             
  1571.             if '.' in res:
  1572.                 return getdouble(res)
  1573.             
  1574.             return getint(res)
  1575.         
  1576.  
  1577.     
  1578.     def grid_columnconfigure(self, index, cnf = { }, **kw):
  1579.         '''Configure column INDEX of a grid.
  1580.  
  1581.         Valid resources are minsize (minimum size of the column),
  1582.         weight (how much does additional space propagate to this column)
  1583.         and pad (how much space to let additionally).'''
  1584.         return self._grid_configure('columnconfigure', index, cnf, kw)
  1585.  
  1586.     columnconfigure = grid_columnconfigure
  1587.     
  1588.     def grid_location(self, x, y):
  1589.         '''Return a tuple of column and row which identify the cell
  1590.         at which the pixel at position X and Y inside the master
  1591.         widget is located.'''
  1592.         if not self._getints(self.tk.call('grid', 'location', self._w, x, y)):
  1593.             pass
  1594.  
  1595.     
  1596.     def grid_propagate(self, flag = _noarg_):
  1597.         '''Set or get the status for propagation of geometry information.
  1598.  
  1599.         A boolean argument specifies whether the geometry information
  1600.         of the slaves will determine the size of this widget. If no argument
  1601.         is given, the current setting will be returned.
  1602.         '''
  1603.         if flag is Misc._noarg_:
  1604.             return self._getboolean(self.tk.call('grid', 'propagate', self._w))
  1605.         else:
  1606.             self.tk.call('grid', 'propagate', self._w, flag)
  1607.  
  1608.     
  1609.     def grid_rowconfigure(self, index, cnf = { }, **kw):
  1610.         '''Configure row INDEX of a grid.
  1611.  
  1612.         Valid resources are minsize (minimum size of the row),
  1613.         weight (how much does additional space propagate to this row)
  1614.         and pad (how much space to let additionally).'''
  1615.         return self._grid_configure('rowconfigure', index, cnf, kw)
  1616.  
  1617.     rowconfigure = grid_rowconfigure
  1618.     
  1619.     def grid_size(self):
  1620.         '''Return a tuple of the number of column and rows in the grid.'''
  1621.         if not self._getints(self.tk.call('grid', 'size', self._w)):
  1622.             pass
  1623.  
  1624.     size = grid_size
  1625.     
  1626.     def grid_slaves(self, row = None, column = None):
  1627.         '''Return a list of all slaves of this widget
  1628.         in its packing order.'''
  1629.         args = ()
  1630.         if row is not None:
  1631.             args = args + ('-row', row)
  1632.         
  1633.         if column is not None:
  1634.             args = args + ('-column', column)
  1635.         
  1636.         return map(self._nametowidget, self.tk.splitlist(self.tk.call(('grid', 'slaves', self._w) + args)))
  1637.  
  1638.     
  1639.     def event_add(self, virtual, *sequences):
  1640.         '''Bind a virtual event VIRTUAL (of the form <<Name>>)
  1641.         to an event SEQUENCE such that the virtual event is triggered
  1642.         whenever SEQUENCE occurs.'''
  1643.         args = ('event', 'add', virtual) + sequences
  1644.         self.tk.call(args)
  1645.  
  1646.     
  1647.     def event_delete(self, virtual, *sequences):
  1648.         '''Unbind a virtual event VIRTUAL from SEQUENCE.'''
  1649.         args = ('event', 'delete', virtual) + sequences
  1650.         self.tk.call(args)
  1651.  
  1652.     
  1653.     def event_generate(self, sequence, **kw):
  1654.         '''Generate an event SEQUENCE. Additional
  1655.         keyword arguments specify parameter of the event
  1656.         (e.g. x, y, rootx, rooty).'''
  1657.         args = ('event', 'generate', self._w, sequence)
  1658.         for k, v in kw.items():
  1659.             args = args + ('-%s' % k, str(v))
  1660.         
  1661.         self.tk.call(args)
  1662.  
  1663.     
  1664.     def event_info(self, virtual = None):
  1665.         '''Return a list of all virtual events or the information
  1666.         about the SEQUENCE bound to the virtual event VIRTUAL.'''
  1667.         return self.tk.splitlist(self.tk.call('event', 'info', virtual))
  1668.  
  1669.     
  1670.     def image_names(self):
  1671.         '''Return a list of all existing image names.'''
  1672.         return self.tk.call('image', 'names')
  1673.  
  1674.     
  1675.     def image_types(self):
  1676.         '''Return a list of all available image types (e.g. phote bitmap).'''
  1677.         return self.tk.call('image', 'types')
  1678.  
  1679.  
  1680.  
  1681. class CallWrapper:
  1682.     '''Internal class. Stores function to call when some user
  1683.     defined Tcl function is called e.g. after an event occurred.'''
  1684.     
  1685.     def __init__(self, func, subst, widget):
  1686.         '''Store FUNC, SUBST and WIDGET as members.'''
  1687.         self.func = func
  1688.         self.subst = subst
  1689.         self.widget = widget
  1690.  
  1691.     
  1692.     def __call__(self, *args):
  1693.         '''Apply first function SUBST to arguments, than FUNC.'''
  1694.         
  1695.         try:
  1696.             if self.subst:
  1697.                 args = self.subst(*args)
  1698.             
  1699.             return self.func(*args)
  1700.         except SystemExit:
  1701.             msg = None
  1702.             raise SystemExit, msg
  1703.         except:
  1704.             self.widget._report_exception()
  1705.  
  1706.  
  1707.  
  1708.  
  1709. class Wm:
  1710.     '''Provides functions for the communication with the window manager.'''
  1711.     
  1712.     def wm_aspect(self, minNumer = None, minDenom = None, maxNumer = None, maxDenom = None):
  1713.         '''Instruct the window manager to set the aspect ratio (width/height)
  1714.         of this widget to be between MINNUMER/MINDENOM and MAXNUMER/MAXDENOM. Return a tuple
  1715.         of the actual values if no argument is given.'''
  1716.         return self._getints(self.tk.call('wm', 'aspect', self._w, minNumer, minDenom, maxNumer, maxDenom))
  1717.  
  1718.     aspect = wm_aspect
  1719.     
  1720.     def wm_attributes(self, *args):
  1721.         '''This subcommand returns or sets platform specific attributes
  1722.  
  1723.         The first form returns a list of the platform specific flags and
  1724.         their values. The second form returns the value for the specific
  1725.         option. The third form sets one or more of the values. The values
  1726.         are as follows:
  1727.  
  1728.         On Windows, -disabled gets or sets whether the window is in a
  1729.         disabled state. -toolwindow gets or sets the style of the window
  1730.         to toolwindow (as defined in the MSDN). -topmost gets or sets
  1731.         whether this is a topmost window (displays above all other
  1732.         windows).
  1733.  
  1734.         On Macintosh, XXXXX
  1735.  
  1736.         On Unix, there are currently no special attribute values.
  1737.         '''
  1738.         args = ('wm', 'attributes', self._w) + args
  1739.         return self.tk.call(args)
  1740.  
  1741.     attributes = wm_attributes
  1742.     
  1743.     def wm_client(self, name = None):
  1744.         '''Store NAME in WM_CLIENT_MACHINE property of this widget. Return
  1745.         current value.'''
  1746.         return self.tk.call('wm', 'client', self._w, name)
  1747.  
  1748.     client = wm_client
  1749.     
  1750.     def wm_colormapwindows(self, *wlist):
  1751.         '''Store list of window names (WLIST) into WM_COLORMAPWINDOWS property
  1752.         of this widget. This list contains windows whose colormaps differ from their
  1753.         parents. Return current list of widgets if WLIST is empty.'''
  1754.         if len(wlist) > 1:
  1755.             wlist = (wlist,)
  1756.         
  1757.         args = ('wm', 'colormapwindows', self._w) + wlist
  1758.         return map(self._nametowidget, self.tk.call(args))
  1759.  
  1760.     colormapwindows = wm_colormapwindows
  1761.     
  1762.     def wm_command(self, value = None):
  1763.         '''Store VALUE in WM_COMMAND property. It is the command
  1764.         which shall be used to invoke the application. Return current
  1765.         command if VALUE is None.'''
  1766.         return self.tk.call('wm', 'command', self._w, value)
  1767.  
  1768.     command = wm_command
  1769.     
  1770.     def wm_deiconify(self):
  1771.         '''Deiconify this widget. If it was never mapped it will not be mapped.
  1772.         On Windows it will raise this widget and give it the focus.'''
  1773.         return self.tk.call('wm', 'deiconify', self._w)
  1774.  
  1775.     deiconify = wm_deiconify
  1776.     
  1777.     def wm_focusmodel(self, model = None):
  1778.         '''Set focus model to MODEL. "active" means that this widget will claim
  1779.         the focus itself, "passive" means that the window manager shall give
  1780.         the focus. Return current focus model if MODEL is None.'''
  1781.         return self.tk.call('wm', 'focusmodel', self._w, model)
  1782.  
  1783.     focusmodel = wm_focusmodel
  1784.     
  1785.     def wm_frame(self):
  1786.         '''Return identifier for decorative frame of this widget if present.'''
  1787.         return self.tk.call('wm', 'frame', self._w)
  1788.  
  1789.     frame = wm_frame
  1790.     
  1791.     def wm_geometry(self, newGeometry = None):
  1792.         '''Set geometry to NEWGEOMETRY of the form =widthxheight+x+y. Return
  1793.         current value if None is given.'''
  1794.         return self.tk.call('wm', 'geometry', self._w, newGeometry)
  1795.  
  1796.     geometry = wm_geometry
  1797.     
  1798.     def wm_grid(self, baseWidth = None, baseHeight = None, widthInc = None, heightInc = None):
  1799.         '''Instruct the window manager that this widget shall only be
  1800.         resized on grid boundaries. WIDTHINC and HEIGHTINC are the width and
  1801.         height of a grid unit in pixels. BASEWIDTH and BASEHEIGHT are the
  1802.         number of grid units requested in Tk_GeometryRequest.'''
  1803.         return self._getints(self.tk.call('wm', 'grid', self._w, baseWidth, baseHeight, widthInc, heightInc))
  1804.  
  1805.     grid = wm_grid
  1806.     
  1807.     def wm_group(self, pathName = None):
  1808.         '''Set the group leader widgets for related widgets to PATHNAME. Return
  1809.         the group leader of this widget if None is given.'''
  1810.         return self.tk.call('wm', 'group', self._w, pathName)
  1811.  
  1812.     group = wm_group
  1813.     
  1814.     def wm_iconbitmap(self, bitmap = None):
  1815.         '''Set bitmap for the iconified widget to BITMAP. Return
  1816.         the bitmap if None is given.'''
  1817.         return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
  1818.  
  1819.     iconbitmap = wm_iconbitmap
  1820.     
  1821.     def wm_iconify(self):
  1822.         '''Display widget as icon.'''
  1823.         return self.tk.call('wm', 'iconify', self._w)
  1824.  
  1825.     iconify = wm_iconify
  1826.     
  1827.     def wm_iconmask(self, bitmap = None):
  1828.         '''Set mask for the icon bitmap of this widget. Return the
  1829.         mask if None is given.'''
  1830.         return self.tk.call('wm', 'iconmask', self._w, bitmap)
  1831.  
  1832.     iconmask = wm_iconmask
  1833.     
  1834.     def wm_iconname(self, newName = None):
  1835.         '''Set the name of the icon for this widget. Return the name if
  1836.         None is given.'''
  1837.         return self.tk.call('wm', 'iconname', self._w, newName)
  1838.  
  1839.     iconname = wm_iconname
  1840.     
  1841.     def wm_iconposition(self, x = None, y = None):
  1842.         '''Set the position of the icon of this widget to X and Y. Return
  1843.         a tuple of the current values of X and X if None is given.'''
  1844.         return self._getints(self.tk.call('wm', 'iconposition', self._w, x, y))
  1845.  
  1846.     iconposition = wm_iconposition
  1847.     
  1848.     def wm_iconwindow(self, pathName = None):
  1849.         '''Set widget PATHNAME to be displayed instead of icon. Return the current
  1850.         value if None is given.'''
  1851.         return self.tk.call('wm', 'iconwindow', self._w, pathName)
  1852.  
  1853.     iconwindow = wm_iconwindow
  1854.     
  1855.     def wm_maxsize(self, width = None, height = None):
  1856.         '''Set max WIDTH and HEIGHT for this widget. If the window is gridded
  1857.         the values are given in grid units. Return the current values if None
  1858.         is given.'''
  1859.         return self._getints(self.tk.call('wm', 'maxsize', self._w, width, height))
  1860.  
  1861.     maxsize = wm_maxsize
  1862.     
  1863.     def wm_minsize(self, width = None, height = None):
  1864.         '''Set min WIDTH and HEIGHT for this widget. If the window is gridded
  1865.         the values are given in grid units. Return the current values if None
  1866.         is given.'''
  1867.         return self._getints(self.tk.call('wm', 'minsize', self._w, width, height))
  1868.  
  1869.     minsize = wm_minsize
  1870.     
  1871.     def wm_overrideredirect(self, boolean = None):
  1872.         '''Instruct the window manager to ignore this widget
  1873.         if BOOLEAN is given with 1. Return the current value if None
  1874.         is given.'''
  1875.         return self._getboolean(self.tk.call('wm', 'overrideredirect', self._w, boolean))
  1876.  
  1877.     overrideredirect = wm_overrideredirect
  1878.     
  1879.     def wm_positionfrom(self, who = None):
  1880.         '''Instruct the window manager that the position of this widget shall
  1881.         be defined by the user if WHO is "user", and by its own policy if WHO is
  1882.         "program".'''
  1883.         return self.tk.call('wm', 'positionfrom', self._w, who)
  1884.  
  1885.     positionfrom = wm_positionfrom
  1886.     
  1887.     def wm_protocol(self, name = None, func = None):
  1888.         '''Bind function FUNC to command NAME for this widget.
  1889.         Return the function bound to NAME if None is given. NAME could be
  1890.         e.g. "WM_SAVE_YOURSELF" or "WM_DELETE_WINDOW".'''
  1891.         if callable(func):
  1892.             command = self._register(func)
  1893.         else:
  1894.             command = func
  1895.         return self.tk.call('wm', 'protocol', self._w, name, command)
  1896.  
  1897.     protocol = wm_protocol
  1898.     
  1899.     def wm_resizable(self, width = None, height = None):
  1900.         '''Instruct the window manager whether this width can be resized
  1901.         in WIDTH or HEIGHT. Both values are boolean values.'''
  1902.         return self.tk.call('wm', 'resizable', self._w, width, height)
  1903.  
  1904.     resizable = wm_resizable
  1905.     
  1906.     def wm_sizefrom(self, who = None):
  1907.         '''Instruct the window manager that the size of this widget shall
  1908.         be defined by the user if WHO is "user", and by its own policy if WHO is
  1909.         "program".'''
  1910.         return self.tk.call('wm', 'sizefrom', self._w, who)
  1911.  
  1912.     sizefrom = wm_sizefrom
  1913.     
  1914.     def wm_state(self, newstate = None):
  1915.         '''Query or set the state of this widget as one of normal, icon,
  1916.         iconic (see wm_iconwindow), withdrawn, or zoomed (Windows only).'''
  1917.         return self.tk.call('wm', 'state', self._w, newstate)
  1918.  
  1919.     state = wm_state
  1920.     
  1921.     def wm_title(self, string = None):
  1922.         '''Set the title of this widget.'''
  1923.         return self.tk.call('wm', 'title', self._w, string)
  1924.  
  1925.     title = wm_title
  1926.     
  1927.     def wm_transient(self, master = None):
  1928.         '''Instruct the window manager that this widget is transient
  1929.         with regard to widget MASTER.'''
  1930.         return self.tk.call('wm', 'transient', self._w, master)
  1931.  
  1932.     transient = wm_transient
  1933.     
  1934.     def wm_withdraw(self):
  1935.         '''Withdraw this widget from the screen such that it is unmapped
  1936.         and forgotten by the window manager. Re-draw it with wm_deiconify.'''
  1937.         return self.tk.call('wm', 'withdraw', self._w)
  1938.  
  1939.     withdraw = wm_withdraw
  1940.  
  1941.  
  1942. class Tk(Misc, Wm):
  1943.     '''Toplevel widget of Tk which represents mostly the main window
  1944.     of an appliation. It has an associated Tcl interpreter.'''
  1945.     _w = '.'
  1946.     
  1947.     def __init__(self, screenName = None, baseName = None, className = 'Tk', useTk = 1, sync = 0, use = None):
  1948.         '''Return a new Toplevel widget on screen SCREENNAME. A new Tcl interpreter will
  1949.         be created. BASENAME will be used for the identification of the profile file (see
  1950.         readprofile).
  1951.         It is constructed from sys.argv[0] without extensions if None is given. CLASSNAME
  1952.         is the name of the widget class.'''
  1953.         self.master = None
  1954.         self.children = { }
  1955.         self._tkloaded = 0
  1956.         self.tk = None
  1957.         if baseName is None:
  1958.             import sys
  1959.             import os
  1960.             baseName = os.path.basename(sys.argv[0])
  1961.             (baseName, ext) = os.path.splitext(baseName)
  1962.             if ext not in ('.py', '.pyc', '.pyo'):
  1963.                 baseName = baseName + ext
  1964.             
  1965.         
  1966.         interactive = 0
  1967.         self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
  1968.         if useTk:
  1969.             self._loadtk()
  1970.         
  1971.         self.readprofile(baseName, className)
  1972.  
  1973.     
  1974.     def loadtk(self):
  1975.         if not self._tkloaded:
  1976.             self.tk.loadtk()
  1977.             self._loadtk()
  1978.         
  1979.  
  1980.     
  1981.     def _loadtk(self):
  1982.         global _default_root
  1983.         self._tkloaded = 1
  1984.         if _MacOS and hasattr(_MacOS, 'SchedParams'):
  1985.             _MacOS.SchedParams(1, 0)
  1986.             self.update()
  1987.         
  1988.         tk_version = self.tk.getvar('tk_version')
  1989.         if tk_version != _tkinter.TK_VERSION:
  1990.             raise RuntimeError, "tk.h version (%s) doesn't match libtk.a version (%s)" % (_tkinter.TK_VERSION, tk_version)
  1991.         
  1992.         tcl_version = str(self.tk.getvar('tcl_version'))
  1993.         if tcl_version != _tkinter.TCL_VERSION:
  1994.             raise RuntimeError, "tcl.h version (%s) doesn't match libtcl.a version (%s)" % (_tkinter.TCL_VERSION, tcl_version)
  1995.         
  1996.         if TkVersion < 4.0:
  1997.             raise RuntimeError, 'Tk 4.0 or higher is required; found Tk %s' % str(TkVersion)
  1998.         
  1999.         if self._tclCommands is None:
  2000.             self._tclCommands = []
  2001.         
  2002.         self.tk.createcommand('tkerror', _tkerror)
  2003.         self.tk.createcommand('exit', _exit)
  2004.         self._tclCommands.append('tkerror')
  2005.         self._tclCommands.append('exit')
  2006.         if _support_default_root and not _default_root:
  2007.             _default_root = self
  2008.         
  2009.         self.protocol('WM_DELETE_WINDOW', self.destroy)
  2010.  
  2011.     
  2012.     def destroy(self):
  2013.         '''Destroy this and all descendants widgets. This will
  2014.         end the application of this Tcl interpreter.'''
  2015.         global _default_root
  2016.         for c in self.children.values():
  2017.             c.destroy()
  2018.         
  2019.         self.tk.call('destroy', self._w)
  2020.         Misc.destroy(self)
  2021.         if _support_default_root and _default_root is self:
  2022.             _default_root = None
  2023.         
  2024.  
  2025.     
  2026.     def readprofile(self, baseName, className):
  2027.         '''Internal function. It reads BASENAME.tcl and CLASSNAME.tcl into
  2028.         the Tcl Interpreter and calls execfile on BASENAME.py and CLASSNAME.py if
  2029.         such a file exists in the home directory.'''
  2030.         import os
  2031.         if os.environ.has_key('HOME'):
  2032.             home = os.environ['HOME']
  2033.         else:
  2034.             home = os.curdir
  2035.         class_tcl = os.path.join(home, '.%s.tcl' % className)
  2036.         class_py = os.path.join(home, '.%s.py' % className)
  2037.         base_tcl = os.path.join(home, '.%s.tcl' % baseName)
  2038.         base_py = os.path.join(home, '.%s.py' % baseName)
  2039.         dir = {
  2040.             'self': self }
  2041.         exec 'from Tkinter import *' in dir
  2042.         if os.path.isfile(class_tcl):
  2043.             self.tk.call('source', class_tcl)
  2044.         
  2045.         if os.path.isfile(class_py):
  2046.             execfile(class_py, dir)
  2047.         
  2048.         if os.path.isfile(base_tcl):
  2049.             self.tk.call('source', base_tcl)
  2050.         
  2051.         if os.path.isfile(base_py):
  2052.             execfile(base_py, dir)
  2053.         
  2054.  
  2055.     
  2056.     def report_callback_exception(self, exc, val, tb):
  2057.         '''Internal function. It reports exception on sys.stderr.'''
  2058.         import traceback
  2059.         import sys
  2060.         sys.stderr.write('Exception in Tkinter callback\n')
  2061.         sys.last_type = exc
  2062.         sys.last_value = val
  2063.         sys.last_traceback = tb
  2064.         traceback.print_exception(exc, val, tb)
  2065.  
  2066.     
  2067.     def __getattr__(self, attr):
  2068.         '''Delegate attribute access to the interpreter object'''
  2069.         return getattr(self.tk, attr)
  2070.  
  2071.  
  2072.  
  2073. def Tcl(screenName = None, baseName = None, className = 'Tk', useTk = 0):
  2074.     return Tk(screenName, baseName, className, useTk)
  2075.  
  2076.  
  2077. class Pack:
  2078.     '''Geometry manager Pack.
  2079.  
  2080.     Base class to use the methods pack_* in every widget.'''
  2081.     
  2082.     def pack_configure(self, cnf = { }, **kw):
  2083.         '''Pack a widget in the parent widget. Use as options:
  2084.         after=widget - pack it after you have packed widget
  2085.         anchor=NSEW (or subset) - position widget according to
  2086.                                   given direction
  2087.                 before=widget - pack it before you will pack widget
  2088.         expand=bool - expand widget if parent size grows
  2089.         fill=NONE or X or Y or BOTH - fill widget if widget grows
  2090.         in=master - use master to contain this widget
  2091.         ipadx=amount - add internal padding in x direction
  2092.         ipady=amount - add internal padding in y direction
  2093.         padx=amount - add padding in x direction
  2094.         pady=amount - add padding in y direction
  2095.         side=TOP or BOTTOM or LEFT or RIGHT -  where to add this widget.
  2096.         '''
  2097.         self.tk.call(('pack', 'configure', self._w) + self._options(cnf, kw))
  2098.  
  2099.     pack = configure = config = pack_configure
  2100.     
  2101.     def pack_forget(self):
  2102.         '''Unmap this widget and do not use it for the packing order.'''
  2103.         self.tk.call('pack', 'forget', self._w)
  2104.  
  2105.     forget = pack_forget
  2106.     
  2107.     def pack_info(self):
  2108.         '''Return information about the packing options
  2109.         for this widget.'''
  2110.         words = self.tk.splitlist(self.tk.call('pack', 'info', self._w))
  2111.         dict = { }
  2112.         for i in range(0, len(words), 2):
  2113.             key = words[i][1:]
  2114.             value = words[i + 1]
  2115.             if value[:1] == '.':
  2116.                 value = self._nametowidget(value)
  2117.             
  2118.             dict[key] = value
  2119.         
  2120.         return dict
  2121.  
  2122.     info = pack_info
  2123.     propagate = pack_propagate = Misc.pack_propagate
  2124.     slaves = pack_slaves = Misc.pack_slaves
  2125.  
  2126.  
  2127. class Place:
  2128.     '''Geometry manager Place.
  2129.  
  2130.     Base class to use the methods place_* in every widget.'''
  2131.     
  2132.     def place_configure(self, cnf = { }, **kw):
  2133.         '''Place a widget in the parent widget. Use as options:
  2134.         in=master - master relative to which the widget is placed.
  2135.         x=amount - locate anchor of this widget at position x of master
  2136.         y=amount - locate anchor of this widget at position y of master
  2137.         relx=amount - locate anchor of this widget between 0.0 and 1.0
  2138.                       relative to width of master (1.0 is right edge)
  2139.             rely=amount - locate anchor of this widget between 0.0 and 1.0
  2140.                       relative to height of master (1.0 is bottom edge)
  2141.             anchor=NSEW (or subset) - position anchor according to given direction
  2142.         width=amount - width of this widget in pixel
  2143.         height=amount - height of this widget in pixel
  2144.         relwidth=amount - width of this widget between 0.0 and 1.0
  2145.                           relative to width of master (1.0 is the same width
  2146.                   as the master)
  2147.             relheight=amount - height of this widget between 0.0 and 1.0
  2148.                            relative to height of master (1.0 is the same
  2149.                    height as the master)
  2150.             bordermode="inside" or "outside" - whether to take border width of master widget
  2151.                                                into account
  2152.             '''
  2153.         for k in [
  2154.             'in_']:
  2155.             if kw.has_key(k):
  2156.                 kw[k[:-1]] = kw[k]
  2157.                 del kw[k]
  2158.                 continue
  2159.         
  2160.         self.tk.call(('place', 'configure', self._w) + self._options(cnf, kw))
  2161.  
  2162.     place = configure = config = place_configure
  2163.     
  2164.     def place_forget(self):
  2165.         '''Unmap this widget.'''
  2166.         self.tk.call('place', 'forget', self._w)
  2167.  
  2168.     forget = place_forget
  2169.     
  2170.     def place_info(self):
  2171.         '''Return information about the placing options
  2172.         for this widget.'''
  2173.         words = self.tk.splitlist(self.tk.call('place', 'info', self._w))
  2174.         dict = { }
  2175.         for i in range(0, len(words), 2):
  2176.             key = words[i][1:]
  2177.             value = words[i + 1]
  2178.             if value[:1] == '.':
  2179.                 value = self._nametowidget(value)
  2180.             
  2181.             dict[key] = value
  2182.         
  2183.         return dict
  2184.  
  2185.     info = place_info
  2186.     slaves = place_slaves = Misc.place_slaves
  2187.  
  2188.  
  2189. class Grid:
  2190.     '''Geometry manager Grid.
  2191.  
  2192.     Base class to use the methods grid_* in every widget.'''
  2193.     
  2194.     def grid_configure(self, cnf = { }, **kw):
  2195.         '''Position a widget in the parent widget in a grid. Use as options:
  2196.         column=number - use cell identified with given column (starting with 0)
  2197.         columnspan=number - this widget will span several columns
  2198.         in=master - use master to contain this widget
  2199.         ipadx=amount - add internal padding in x direction
  2200.         ipady=amount - add internal padding in y direction
  2201.         padx=amount - add padding in x direction
  2202.         pady=amount - add padding in y direction
  2203.         row=number - use cell identified with given row (starting with 0)
  2204.         rowspan=number - this widget will span several rows
  2205.         sticky=NSEW - if cell is larger on which sides will this
  2206.                       widget stick to the cell boundary
  2207.         '''
  2208.         self.tk.call(('grid', 'configure', self._w) + self._options(cnf, kw))
  2209.  
  2210.     grid = configure = config = grid_configure
  2211.     bbox = grid_bbox = Misc.grid_bbox
  2212.     columnconfigure = grid_columnconfigure = Misc.grid_columnconfigure
  2213.     
  2214.     def grid_forget(self):
  2215.         '''Unmap this widget.'''
  2216.         self.tk.call('grid', 'forget', self._w)
  2217.  
  2218.     forget = grid_forget
  2219.     
  2220.     def grid_remove(self):
  2221.         '''Unmap this widget but remember the grid options.'''
  2222.         self.tk.call('grid', 'remove', self._w)
  2223.  
  2224.     
  2225.     def grid_info(self):
  2226.         '''Return information about the options
  2227.         for positioning this widget in a grid.'''
  2228.         words = self.tk.splitlist(self.tk.call('grid', 'info', self._w))
  2229.         dict = { }
  2230.         for i in range(0, len(words), 2):
  2231.             key = words[i][1:]
  2232.             value = words[i + 1]
  2233.             if value[:1] == '.':
  2234.                 value = self._nametowidget(value)
  2235.             
  2236.             dict[key] = value
  2237.         
  2238.         return dict
  2239.  
  2240.     info = grid_info
  2241.     location = grid_location = Misc.grid_location
  2242.     propagate = grid_propagate = Misc.grid_propagate
  2243.     rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
  2244.     size = grid_size = Misc.grid_size
  2245.     slaves = grid_slaves = Misc.grid_slaves
  2246.  
  2247.  
  2248. class BaseWidget(Misc):
  2249.     '''Internal class.'''
  2250.     
  2251.     def _setup(self, master, cnf):
  2252.         '''Internal function. Sets up information about children.'''
  2253.         global _default_root
  2254.         if _support_default_root:
  2255.             if not master:
  2256.                 if not _default_root:
  2257.                     _default_root = Tk()
  2258.                 
  2259.                 master = _default_root
  2260.             
  2261.         
  2262.         self.master = master
  2263.         self.tk = master.tk
  2264.         name = None
  2265.         if cnf.has_key('name'):
  2266.             name = cnf['name']
  2267.             del cnf['name']
  2268.         
  2269.         if not name:
  2270.             name = repr(id(self))
  2271.         
  2272.         self._name = name
  2273.         if master._w == '.':
  2274.             self._w = '.' + name
  2275.         else:
  2276.             self._w = master._w + '.' + name
  2277.         self.children = { }
  2278.         if self.master.children.has_key(self._name):
  2279.             self.master.children[self._name].destroy()
  2280.         
  2281.         self.master.children[self._name] = self
  2282.  
  2283.     
  2284.     def __init__(self, master, widgetName, cnf = { }, kw = { }, extra = ()):
  2285.         '''Construct a widget with the parent widget MASTER, a name WIDGETNAME
  2286.         and appropriate options.'''
  2287.         if kw:
  2288.             cnf = _cnfmerge((cnf, kw))
  2289.         
  2290.         self.widgetName = widgetName
  2291.         BaseWidget._setup(self, master, cnf)
  2292.         classes = []
  2293.         for k in cnf.keys():
  2294.             if type(k) is ClassType:
  2295.                 classes.append((k, cnf[k]))
  2296.                 del cnf[k]
  2297.                 continue
  2298.         
  2299.         self.tk.call((widgetName, self._w) + extra + self._options(cnf))
  2300.         for k, v in classes:
  2301.             k.configure(self, v)
  2302.         
  2303.  
  2304.     
  2305.     def destroy(self):
  2306.         '''Destroy this and all descendants widgets.'''
  2307.         for c in self.children.values():
  2308.             c.destroy()
  2309.         
  2310.         if self.master.children.has_key(self._name):
  2311.             del self.master.children[self._name]
  2312.         
  2313.         self.tk.call('destroy', self._w)
  2314.         Misc.destroy(self)
  2315.  
  2316.     
  2317.     def _do(self, name, args = ()):
  2318.         return self.tk.call((self._w, name) + args)
  2319.  
  2320.  
  2321.  
  2322. class Widget(BaseWidget, Pack, Place, Grid):
  2323.     '''Internal class.
  2324.  
  2325.     Base class for a widget which can be positioned with the geometry managers
  2326.     Pack, Place or Grid.'''
  2327.     pass
  2328.  
  2329.  
  2330. class Toplevel(BaseWidget, Wm):
  2331.     '''Toplevel widget, e.g. for dialogs.'''
  2332.     
  2333.     def __init__(self, master = None, cnf = { }, **kw):
  2334.         '''Construct a toplevel widget with the parent MASTER.
  2335.  
  2336.         Valid resource names: background, bd, bg, borderwidth, class,
  2337.         colormap, container, cursor, height, highlightbackground,
  2338.         highlightcolor, highlightthickness, menu, relief, screen, takefocus,
  2339.         use, visual, width.'''
  2340.         if kw:
  2341.             cnf = _cnfmerge((cnf, kw))
  2342.         
  2343.         extra = ()
  2344.         for wmkey in [
  2345.             'screen',
  2346.             'class_',
  2347.             'class',
  2348.             'visual',
  2349.             'colormap']:
  2350.             if cnf.has_key(wmkey):
  2351.                 val = cnf[wmkey]
  2352.                 if wmkey[-1] == '_':
  2353.                     opt = '-' + wmkey[:-1]
  2354.                 else:
  2355.                     opt = '-' + wmkey
  2356.                 extra = extra + (opt, val)
  2357.                 del cnf[wmkey]
  2358.                 continue
  2359.         
  2360.         BaseWidget.__init__(self, master, 'toplevel', cnf, { }, extra)
  2361.         root = self._root()
  2362.         self.iconname(root.iconname())
  2363.         self.title(root.title())
  2364.         self.protocol('WM_DELETE_WINDOW', self.destroy)
  2365.  
  2366.  
  2367.  
  2368. class Button(Widget):
  2369.     '''Button widget.'''
  2370.     
  2371.     def __init__(self, master = None, cnf = { }, **kw):
  2372.         '''Construct a button widget with the parent MASTER.
  2373.  
  2374.         STANDARD OPTIONS
  2375.  
  2376.             activebackground, activeforeground, anchor,
  2377.             background, bitmap, borderwidth, cursor,
  2378.             disabledforeground, font, foreground
  2379.             highlightbackground, highlightcolor,
  2380.             highlightthickness, image, justify,
  2381.             padx, pady, relief, repeatdelay,
  2382.             repeatinterval, takefocus, text,
  2383.             textvariable, underline, wraplength
  2384.  
  2385.         WIDGET-SPECIFIC OPTIONS
  2386.  
  2387.             command, compound, default, height,
  2388.             overrelief, state, width
  2389.         '''
  2390.         Widget.__init__(self, master, 'button', cnf, kw)
  2391.  
  2392.     
  2393.     def tkButtonEnter(self, *dummy):
  2394.         self.tk.call('tkButtonEnter', self._w)
  2395.  
  2396.     
  2397.     def tkButtonLeave(self, *dummy):
  2398.         self.tk.call('tkButtonLeave', self._w)
  2399.  
  2400.     
  2401.     def tkButtonDown(self, *dummy):
  2402.         self.tk.call('tkButtonDown', self._w)
  2403.  
  2404.     
  2405.     def tkButtonUp(self, *dummy):
  2406.         self.tk.call('tkButtonUp', self._w)
  2407.  
  2408.     
  2409.     def tkButtonInvoke(self, *dummy):
  2410.         self.tk.call('tkButtonInvoke', self._w)
  2411.  
  2412.     
  2413.     def flash(self):
  2414.         """Flash the button.
  2415.  
  2416.         This is accomplished by redisplaying
  2417.         the button several times, alternating between active and
  2418.         normal colors. At the end of the flash the button is left
  2419.         in the same normal/active state as when the command was
  2420.         invoked. This command is ignored if the button's state is
  2421.         disabled.
  2422.         """
  2423.         self.tk.call(self._w, 'flash')
  2424.  
  2425.     
  2426.     def invoke(self):
  2427.         """Invoke the command associated with the button.
  2428.  
  2429.         The return value is the return value from the command,
  2430.         or an empty string if there is no command associated with
  2431.         the button. This command is ignored if the button's state
  2432.         is disabled.
  2433.         """
  2434.         return self.tk.call(self._w, 'invoke')
  2435.  
  2436.  
  2437.  
  2438. def AtEnd():
  2439.     return 'end'
  2440.  
  2441.  
  2442. def AtInsert(*args):
  2443.     s = 'insert'
  2444.     for a in args:
  2445.         if a:
  2446.             s = s + ' ' + a
  2447.             continue
  2448.     
  2449.     return s
  2450.  
  2451.  
  2452. def AtSelFirst():
  2453.     return 'sel.first'
  2454.  
  2455.  
  2456. def AtSelLast():
  2457.     return 'sel.last'
  2458.  
  2459.  
  2460. def At(x, y = None):
  2461.     if y is None:
  2462.         return '@%r' % (x,)
  2463.     else:
  2464.         return '@%r,%r' % (x, y)
  2465.  
  2466.  
  2467. class Canvas(Widget):
  2468.     '''Canvas widget to display graphical elements like lines or text.'''
  2469.     
  2470.     def __init__(self, master = None, cnf = { }, **kw):
  2471.         '''Construct a canvas widget with the parent MASTER.
  2472.  
  2473.         Valid resource names: background, bd, bg, borderwidth, closeenough,
  2474.         confine, cursor, height, highlightbackground, highlightcolor,
  2475.         highlightthickness, insertbackground, insertborderwidth,
  2476.         insertofftime, insertontime, insertwidth, offset, relief,
  2477.         scrollregion, selectbackground, selectborderwidth, selectforeground,
  2478.         state, takefocus, width, xscrollcommand, xscrollincrement,
  2479.         yscrollcommand, yscrollincrement.'''
  2480.         Widget.__init__(self, master, 'canvas', cnf, kw)
  2481.  
  2482.     
  2483.     def addtag(self, *args):
  2484.         '''Internal function.'''
  2485.         self.tk.call((self._w, 'addtag') + args)
  2486.  
  2487.     
  2488.     def addtag_above(self, newtag, tagOrId):
  2489.         '''Add tag NEWTAG to all items above TAGORID.'''
  2490.         self.addtag(newtag, 'above', tagOrId)
  2491.  
  2492.     
  2493.     def addtag_all(self, newtag):
  2494.         '''Add tag NEWTAG to all items.'''
  2495.         self.addtag(newtag, 'all')
  2496.  
  2497.     
  2498.     def addtag_below(self, newtag, tagOrId):
  2499.         '''Add tag NEWTAG to all items below TAGORID.'''
  2500.         self.addtag(newtag, 'below', tagOrId)
  2501.  
  2502.     
  2503.     def addtag_closest(self, newtag, x, y, halo = None, start = None):
  2504.         '''Add tag NEWTAG to item which is closest to pixel at X, Y.
  2505.         If several match take the top-most.
  2506.         All items closer than HALO are considered overlapping (all are
  2507.         closests). If START is specified the next below this tag is taken.'''
  2508.         self.addtag(newtag, 'closest', x, y, halo, start)
  2509.  
  2510.     
  2511.     def addtag_enclosed(self, newtag, x1, y1, x2, y2):
  2512.         '''Add tag NEWTAG to all items in the rectangle defined
  2513.         by X1,Y1,X2,Y2.'''
  2514.         self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
  2515.  
  2516.     
  2517.     def addtag_overlapping(self, newtag, x1, y1, x2, y2):
  2518.         '''Add tag NEWTAG to all items which overlap the rectangle
  2519.         defined by X1,Y1,X2,Y2.'''
  2520.         self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
  2521.  
  2522.     
  2523.     def addtag_withtag(self, newtag, tagOrId):
  2524.         '''Add tag NEWTAG to all items with TAGORID.'''
  2525.         self.addtag(newtag, 'withtag', tagOrId)
  2526.  
  2527.     
  2528.     def bbox(self, *args):
  2529.         '''Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
  2530.         which encloses all items with tags specified as arguments.'''
  2531.         if not self._getints(self.tk.call((self._w, 'bbox') + args)):
  2532.             pass
  2533.  
  2534.     
  2535.     def tag_unbind(self, tagOrId, sequence, funcid = None):
  2536.         '''Unbind for all items with TAGORID for event SEQUENCE  the
  2537.         function identified with FUNCID.'''
  2538.         self.tk.call(self._w, 'bind', tagOrId, sequence, '')
  2539.         if funcid:
  2540.             self.deletecommand(funcid)
  2541.         
  2542.  
  2543.     
  2544.     def tag_bind(self, tagOrId, sequence = None, func = None, add = None):
  2545.         '''Bind to all items with TAGORID at event SEQUENCE a call to function FUNC.
  2546.  
  2547.         An additional boolean parameter ADD specifies whether FUNC will be
  2548.         called additionally to the other bound function or whether it will
  2549.         replace the previous function. See bind for the return value.'''
  2550.         return self._bind((self._w, 'bind', tagOrId), sequence, func, add)
  2551.  
  2552.     
  2553.     def canvasx(self, screenx, gridspacing = None):
  2554.         '''Return the canvas x coordinate of pixel position SCREENX rounded
  2555.         to nearest multiple of GRIDSPACING units.'''
  2556.         return getdouble(self.tk.call(self._w, 'canvasx', screenx, gridspacing))
  2557.  
  2558.     
  2559.     def canvasy(self, screeny, gridspacing = None):
  2560.         '''Return the canvas y coordinate of pixel position SCREENY rounded
  2561.         to nearest multiple of GRIDSPACING units.'''
  2562.         return getdouble(self.tk.call(self._w, 'canvasy', screeny, gridspacing))
  2563.  
  2564.     
  2565.     def coords(self, *args):
  2566.         '''Return a list of coordinates for the item given in ARGS.'''
  2567.         return map(getdouble, self.tk.splitlist(self.tk.call((self._w, 'coords') + args)))
  2568.  
  2569.     
  2570.     def _create(self, itemType, args, kw):
  2571.         '''Internal function.'''
  2572.         args = _flatten(args)
  2573.         cnf = args[-1]
  2574.         if type(cnf) in (DictionaryType, TupleType):
  2575.             args = args[:-1]
  2576.         else:
  2577.             cnf = { }
  2578.         return getint(self.tk.call(self._w, 'create', itemType, *args + self._options(cnf, kw)))
  2579.  
  2580.     
  2581.     def create_arc(self, *args, **kw):
  2582.         '''Create arc shaped region with coordinates x1,y1,x2,y2.'''
  2583.         return self._create('arc', args, kw)
  2584.  
  2585.     
  2586.     def create_bitmap(self, *args, **kw):
  2587.         '''Create bitmap with coordinates x1,y1.'''
  2588.         return self._create('bitmap', args, kw)
  2589.  
  2590.     
  2591.     def create_image(self, *args, **kw):
  2592.         '''Create image item with coordinates x1,y1.'''
  2593.         return self._create('image', args, kw)
  2594.  
  2595.     
  2596.     def create_line(self, *args, **kw):
  2597.         '''Create line with coordinates x1,y1,...,xn,yn.'''
  2598.         return self._create('line', args, kw)
  2599.  
  2600.     
  2601.     def create_oval(self, *args, **kw):
  2602.         '''Create oval with coordinates x1,y1,x2,y2.'''
  2603.         return self._create('oval', args, kw)
  2604.  
  2605.     
  2606.     def create_polygon(self, *args, **kw):
  2607.         '''Create polygon with coordinates x1,y1,...,xn,yn.'''
  2608.         return self._create('polygon', args, kw)
  2609.  
  2610.     
  2611.     def create_rectangle(self, *args, **kw):
  2612.         '''Create rectangle with coordinates x1,y1,x2,y2.'''
  2613.         return self._create('rectangle', args, kw)
  2614.  
  2615.     
  2616.     def create_text(self, *args, **kw):
  2617.         '''Create text with coordinates x1,y1.'''
  2618.         return self._create('text', args, kw)
  2619.  
  2620.     
  2621.     def create_window(self, *args, **kw):
  2622.         '''Create window with coordinates x1,y1,x2,y2.'''
  2623.         return self._create('window', args, kw)
  2624.  
  2625.     
  2626.     def dchars(self, *args):
  2627.         '''Delete characters of text items identified by tag or id in ARGS (possibly
  2628.         several times) from FIRST to LAST character (including).'''
  2629.         self.tk.call((self._w, 'dchars') + args)
  2630.  
  2631.     
  2632.     def delete(self, *args):
  2633.         '''Delete items identified by all tag or ids contained in ARGS.'''
  2634.         self.tk.call((self._w, 'delete') + args)
  2635.  
  2636.     
  2637.     def dtag(self, *args):
  2638.         '''Delete tag or id given as last arguments in ARGS from items
  2639.         identified by first argument in ARGS.'''
  2640.         self.tk.call((self._w, 'dtag') + args)
  2641.  
  2642.     
  2643.     def find(self, *args):
  2644.         '''Internal function.'''
  2645.         if not self._getints(self.tk.call((self._w, 'find') + args)):
  2646.             pass
  2647.         return ()
  2648.  
  2649.     
  2650.     def find_above(self, tagOrId):
  2651.         '''Return items above TAGORID.'''
  2652.         return self.find('above', tagOrId)
  2653.  
  2654.     
  2655.     def find_all(self):
  2656.         '''Return all items.'''
  2657.         return self.find('all')
  2658.  
  2659.     
  2660.     def find_below(self, tagOrId):
  2661.         '''Return all items below TAGORID.'''
  2662.         return self.find('below', tagOrId)
  2663.  
  2664.     
  2665.     def find_closest(self, x, y, halo = None, start = None):
  2666.         '''Return item which is closest to pixel at X, Y.
  2667.         If several match take the top-most.
  2668.         All items closer than HALO are considered overlapping (all are
  2669.         closests). If START is specified the next below this tag is taken.'''
  2670.         return self.find('closest', x, y, halo, start)
  2671.  
  2672.     
  2673.     def find_enclosed(self, x1, y1, x2, y2):
  2674.         '''Return all items in rectangle defined
  2675.         by X1,Y1,X2,Y2.'''
  2676.         return self.find('enclosed', x1, y1, x2, y2)
  2677.  
  2678.     
  2679.     def find_overlapping(self, x1, y1, x2, y2):
  2680.         '''Return all items which overlap the rectangle
  2681.         defined by X1,Y1,X2,Y2.'''
  2682.         return self.find('overlapping', x1, y1, x2, y2)
  2683.  
  2684.     
  2685.     def find_withtag(self, tagOrId):
  2686.         '''Return all items with TAGORID.'''
  2687.         return self.find('withtag', tagOrId)
  2688.  
  2689.     
  2690.     def focus(self, *args):
  2691.         '''Set focus to the first item specified in ARGS.'''
  2692.         return self.tk.call((self._w, 'focus') + args)
  2693.  
  2694.     
  2695.     def gettags(self, *args):
  2696.         '''Return tags associated with the first item specified in ARGS.'''
  2697.         return self.tk.splitlist(self.tk.call((self._w, 'gettags') + args))
  2698.  
  2699.     
  2700.     def icursor(self, *args):
  2701.         '''Set cursor at position POS in the item identified by TAGORID.
  2702.         In ARGS TAGORID must be first.'''
  2703.         self.tk.call((self._w, 'icursor') + args)
  2704.  
  2705.     
  2706.     def index(self, *args):
  2707.         '''Return position of cursor as integer in item specified in ARGS.'''
  2708.         return getint(self.tk.call((self._w, 'index') + args))
  2709.  
  2710.     
  2711.     def insert(self, *args):
  2712.         '''Insert TEXT in item TAGORID at position POS. ARGS must
  2713.         be TAGORID POS TEXT.'''
  2714.         self.tk.call((self._w, 'insert') + args)
  2715.  
  2716.     
  2717.     def itemcget(self, tagOrId, option):
  2718.         '''Return the resource value for an OPTION for item TAGORID.'''
  2719.         return self.tk.call((self._w, 'itemcget') + (tagOrId, '-' + option))
  2720.  
  2721.     
  2722.     def itemconfigure(self, tagOrId, cnf = None, **kw):
  2723.         '''Configure resources of an item TAGORID.
  2724.  
  2725.         The values for resources are specified as keyword
  2726.         arguments. To get an overview about
  2727.         the allowed keyword arguments call the method without arguments.
  2728.         '''
  2729.         return self._configure(('itemconfigure', tagOrId), cnf, kw)
  2730.  
  2731.     itemconfig = itemconfigure
  2732.     
  2733.     def tag_lower(self, *args):
  2734.         '''Lower an item TAGORID given in ARGS
  2735.         (optional below another item).'''
  2736.         self.tk.call((self._w, 'lower') + args)
  2737.  
  2738.     lower = tag_lower
  2739.     
  2740.     def move(self, *args):
  2741.         '''Move an item TAGORID given in ARGS.'''
  2742.         self.tk.call((self._w, 'move') + args)
  2743.  
  2744.     
  2745.     def postscript(self, cnf = { }, **kw):
  2746.         '''Print the contents of the canvas to a postscript
  2747.         file. Valid options: colormap, colormode, file, fontmap,
  2748.         height, pageanchor, pageheight, pagewidth, pagex, pagey,
  2749.         rotate, witdh, x, y.'''
  2750.         return self.tk.call((self._w, 'postscript') + self._options(cnf, kw))
  2751.  
  2752.     
  2753.     def tag_raise(self, *args):
  2754.         '''Raise an item TAGORID given in ARGS
  2755.         (optional above another item).'''
  2756.         self.tk.call((self._w, 'raise') + args)
  2757.  
  2758.     lift = tkraise = tag_raise
  2759.     
  2760.     def scale(self, *args):
  2761.         '''Scale item TAGORID with XORIGIN, YORIGIN, XSCALE, YSCALE.'''
  2762.         self.tk.call((self._w, 'scale') + args)
  2763.  
  2764.     
  2765.     def scan_mark(self, x, y):
  2766.         '''Remember the current X, Y coordinates.'''
  2767.         self.tk.call(self._w, 'scan', 'mark', x, y)
  2768.  
  2769.     
  2770.     def scan_dragto(self, x, y, gain = 10):
  2771.         '''Adjust the view of the canvas to GAIN times the
  2772.         difference between X and Y and the coordinates given in
  2773.         scan_mark.'''
  2774.         self.tk.call(self._w, 'scan', 'dragto', x, y, gain)
  2775.  
  2776.     
  2777.     def select_adjust(self, tagOrId, index):
  2778.         '''Adjust the end of the selection near the cursor of an item TAGORID to index.'''
  2779.         self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
  2780.  
  2781.     
  2782.     def select_clear(self):
  2783.         '''Clear the selection if it is in this widget.'''
  2784.         self.tk.call(self._w, 'select', 'clear')
  2785.  
  2786.     
  2787.     def select_from(self, tagOrId, index):
  2788.         '''Set the fixed end of a selection in item TAGORID to INDEX.'''
  2789.         self.tk.call(self._w, 'select', 'from', tagOrId, index)
  2790.  
  2791.     
  2792.     def select_item(self):
  2793.         '''Return the item which has the selection.'''
  2794.         if not self.tk.call(self._w, 'select', 'item'):
  2795.             pass
  2796.  
  2797.     
  2798.     def select_to(self, tagOrId, index):
  2799.         '''Set the variable end of a selection in item TAGORID to INDEX.'''
  2800.         self.tk.call(self._w, 'select', 'to', tagOrId, index)
  2801.  
  2802.     
  2803.     def type(self, tagOrId):
  2804.         '''Return the type of the item TAGORID.'''
  2805.         if not self.tk.call(self._w, 'type', tagOrId):
  2806.             pass
  2807.  
  2808.     
  2809.     def xview(self, *args):
  2810.         '''Query and change horizontal position of the view.'''
  2811.         if not args:
  2812.             return self._getdoubles(self.tk.call(self._w, 'xview'))
  2813.         
  2814.         self.tk.call((self._w, 'xview') + args)
  2815.  
  2816.     
  2817.     def xview_moveto(self, fraction):
  2818.         '''Adjusts the view in the window so that FRACTION of the
  2819.         total width of the canvas is off-screen to the left.'''
  2820.         self.tk.call(self._w, 'xview', 'moveto', fraction)
  2821.  
  2822.     
  2823.     def xview_scroll(self, number, what):
  2824.         '''Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT).'''
  2825.         self.tk.call(self._w, 'xview', 'scroll', number, what)
  2826.  
  2827.     
  2828.     def yview(self, *args):
  2829.         '''Query and change vertical position of the view.'''
  2830.         if not args:
  2831.             return self._getdoubles(self.tk.call(self._w, 'yview'))
  2832.         
  2833.         self.tk.call((self._w, 'yview') + args)
  2834.  
  2835.     
  2836.     def yview_moveto(self, fraction):
  2837.         '''Adjusts the view in the window so that FRACTION of the
  2838.         total height of the canvas is off-screen to the top.'''
  2839.         self.tk.call(self._w, 'yview', 'moveto', fraction)
  2840.  
  2841.     
  2842.     def yview_scroll(self, number, what):
  2843.         '''Shift the y-view according to NUMBER which is measured in "units" or "pages" (WHAT).'''
  2844.         self.tk.call(self._w, 'yview', 'scroll', number, what)
  2845.  
  2846.  
  2847.  
  2848. class Checkbutton(Widget):
  2849.     '''Checkbutton widget which is either in on- or off-state.'''
  2850.     
  2851.     def __init__(self, master = None, cnf = { }, **kw):
  2852.         '''Construct a checkbutton widget with the parent MASTER.
  2853.  
  2854.         Valid resource names: activebackground, activeforeground, anchor,
  2855.         background, bd, bg, bitmap, borderwidth, command, cursor,
  2856.         disabledforeground, fg, font, foreground, height,
  2857.         highlightbackground, highlightcolor, highlightthickness, image,
  2858.         indicatoron, justify, offvalue, onvalue, padx, pady, relief,
  2859.         selectcolor, selectimage, state, takefocus, text, textvariable,
  2860.         underline, variable, width, wraplength.'''
  2861.         Widget.__init__(self, master, 'checkbutton', cnf, kw)
  2862.  
  2863.     
  2864.     def deselect(self):
  2865.         '''Put the button in off-state.'''
  2866.         self.tk.call(self._w, 'deselect')
  2867.  
  2868.     
  2869.     def flash(self):
  2870.         '''Flash the button.'''
  2871.         self.tk.call(self._w, 'flash')
  2872.  
  2873.     
  2874.     def invoke(self):
  2875.         '''Toggle the button and invoke a command if given as resource.'''
  2876.         return self.tk.call(self._w, 'invoke')
  2877.  
  2878.     
  2879.     def select(self):
  2880.         '''Put the button in on-state.'''
  2881.         self.tk.call(self._w, 'select')
  2882.  
  2883.     
  2884.     def toggle(self):
  2885.         '''Toggle the button.'''
  2886.         self.tk.call(self._w, 'toggle')
  2887.  
  2888.  
  2889.  
  2890. class Entry(Widget):
  2891.     '''Entry widget which allows to display simple text.'''
  2892.     
  2893.     def __init__(self, master = None, cnf = { }, **kw):
  2894.         '''Construct an entry widget with the parent MASTER.
  2895.  
  2896.         Valid resource names: background, bd, bg, borderwidth, cursor,
  2897.         exportselection, fg, font, foreground, highlightbackground,
  2898.         highlightcolor, highlightthickness, insertbackground,
  2899.         insertborderwidth, insertofftime, insertontime, insertwidth,
  2900.         invalidcommand, invcmd, justify, relief, selectbackground,
  2901.         selectborderwidth, selectforeground, show, state, takefocus,
  2902.         textvariable, validate, validatecommand, vcmd, width,
  2903.         xscrollcommand.'''
  2904.         Widget.__init__(self, master, 'entry', cnf, kw)
  2905.  
  2906.     
  2907.     def delete(self, first, last = None):
  2908.         '''Delete text from FIRST to LAST (not included).'''
  2909.         self.tk.call(self._w, 'delete', first, last)
  2910.  
  2911.     
  2912.     def get(self):
  2913.         '''Return the text.'''
  2914.         return self.tk.call(self._w, 'get')
  2915.  
  2916.     
  2917.     def icursor(self, index):
  2918.         '''Insert cursor at INDEX.'''
  2919.         self.tk.call(self._w, 'icursor', index)
  2920.  
  2921.     
  2922.     def index(self, index):
  2923.         '''Return position of cursor.'''
  2924.         return getint(self.tk.call(self._w, 'index', index))
  2925.  
  2926.     
  2927.     def insert(self, index, string):
  2928.         '''Insert STRING at INDEX.'''
  2929.         self.tk.call(self._w, 'insert', index, string)
  2930.  
  2931.     
  2932.     def scan_mark(self, x):
  2933.         '''Remember the current X, Y coordinates.'''
  2934.         self.tk.call(self._w, 'scan', 'mark', x)
  2935.  
  2936.     
  2937.     def scan_dragto(self, x):
  2938.         '''Adjust the view of the canvas to 10 times the
  2939.         difference between X and Y and the coordinates given in
  2940.         scan_mark.'''
  2941.         self.tk.call(self._w, 'scan', 'dragto', x)
  2942.  
  2943.     
  2944.     def selection_adjust(self, index):
  2945.         '''Adjust the end of the selection near the cursor to INDEX.'''
  2946.         self.tk.call(self._w, 'selection', 'adjust', index)
  2947.  
  2948.     select_adjust = selection_adjust
  2949.     
  2950.     def selection_clear(self):
  2951.         '''Clear the selection if it is in this widget.'''
  2952.         self.tk.call(self._w, 'selection', 'clear')
  2953.  
  2954.     select_clear = selection_clear
  2955.     
  2956.     def selection_from(self, index):
  2957.         '''Set the fixed end of a selection to INDEX.'''
  2958.         self.tk.call(self._w, 'selection', 'from', index)
  2959.  
  2960.     select_from = selection_from
  2961.     
  2962.     def selection_present(self):
  2963.         '''Return whether the widget has the selection.'''
  2964.         return self.tk.getboolean(self.tk.call(self._w, 'selection', 'present'))
  2965.  
  2966.     select_present = selection_present
  2967.     
  2968.     def selection_range(self, start, end):
  2969.         '''Set the selection from START to END (not included).'''
  2970.         self.tk.call(self._w, 'selection', 'range', start, end)
  2971.  
  2972.     select_range = selection_range
  2973.     
  2974.     def selection_to(self, index):
  2975.         '''Set the variable end of a selection to INDEX.'''
  2976.         self.tk.call(self._w, 'selection', 'to', index)
  2977.  
  2978.     select_to = selection_to
  2979.     
  2980.     def xview(self, index):
  2981.         '''Query and change horizontal position of the view.'''
  2982.         self.tk.call(self._w, 'xview', index)
  2983.  
  2984.     
  2985.     def xview_moveto(self, fraction):
  2986.         '''Adjust the view in the window so that FRACTION of the
  2987.         total width of the entry is off-screen to the left.'''
  2988.         self.tk.call(self._w, 'xview', 'moveto', fraction)
  2989.  
  2990.     
  2991.     def xview_scroll(self, number, what):
  2992.         '''Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT).'''
  2993.         self.tk.call(self._w, 'xview', 'scroll', number, what)
  2994.  
  2995.  
  2996.  
  2997. class Frame(Widget):
  2998.     '''Frame widget which may contain other widgets and can have a 3D border.'''
  2999.     
  3000.     def __init__(self, master = None, cnf = { }, **kw):
  3001.         '''Construct a frame widget with the parent MASTER.
  3002.  
  3003.         Valid resource names: background, bd, bg, borderwidth, class,
  3004.         colormap, container, cursor, height, highlightbackground,
  3005.         highlightcolor, highlightthickness, relief, takefocus, visual, width.'''
  3006.         cnf = _cnfmerge((cnf, kw))
  3007.         extra = ()
  3008.         if cnf.has_key('class_'):
  3009.             extra = ('-class', cnf['class_'])
  3010.             del cnf['class_']
  3011.         elif cnf.has_key('class'):
  3012.             extra = ('-class', cnf['class'])
  3013.             del cnf['class']
  3014.         
  3015.         Widget.__init__(self, master, 'frame', cnf, { }, extra)
  3016.  
  3017.  
  3018.  
  3019. class Label(Widget):
  3020.     '''Label widget which can display text and bitmaps.'''
  3021.     
  3022.     def __init__(self, master = None, cnf = { }, **kw):
  3023.         '''Construct a label widget with the parent MASTER.
  3024.  
  3025.         STANDARD OPTIONS
  3026.  
  3027.             activebackground, activeforeground, anchor,
  3028.             background, bitmap, borderwidth, cursor,
  3029.             disabledforeground, font, foreground,
  3030.             highlightbackground, highlightcolor,
  3031.             highlightthickness, image, justify,
  3032.             padx, pady, relief, takefocus, text,
  3033.             textvariable, underline, wraplength
  3034.  
  3035.         WIDGET-SPECIFIC OPTIONS
  3036.  
  3037.             height, state, width
  3038.  
  3039.         '''
  3040.         Widget.__init__(self, master, 'label', cnf, kw)
  3041.  
  3042.  
  3043.  
  3044. class Listbox(Widget):
  3045.     '''Listbox widget which can display a list of strings.'''
  3046.     
  3047.     def __init__(self, master = None, cnf = { }, **kw):
  3048.         '''Construct a listbox widget with the parent MASTER.
  3049.  
  3050.         Valid resource names: background, bd, bg, borderwidth, cursor,
  3051.         exportselection, fg, font, foreground, height, highlightbackground,
  3052.         highlightcolor, highlightthickness, relief, selectbackground,
  3053.         selectborderwidth, selectforeground, selectmode, setgrid, takefocus,
  3054.         width, xscrollcommand, yscrollcommand, listvariable.'''
  3055.         Widget.__init__(self, master, 'listbox', cnf, kw)
  3056.  
  3057.     
  3058.     def activate(self, index):
  3059.         '''Activate item identified by INDEX.'''
  3060.         self.tk.call(self._w, 'activate', index)
  3061.  
  3062.     
  3063.     def bbox(self, *args):
  3064.         '''Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
  3065.         which encloses the item identified by index in ARGS.'''
  3066.         if not self._getints(self.tk.call((self._w, 'bbox') + args)):
  3067.             pass
  3068.  
  3069.     
  3070.     def curselection(self):
  3071.         '''Return list of indices of currently selected item.'''
  3072.         return self.tk.splitlist(self.tk.call(self._w, 'curselection'))
  3073.  
  3074.     
  3075.     def delete(self, first, last = None):
  3076.         '''Delete items from FIRST to LAST (not included).'''
  3077.         self.tk.call(self._w, 'delete', first, last)
  3078.  
  3079.     
  3080.     def get(self, first, last = None):
  3081.         '''Get list of items from FIRST to LAST (not included).'''
  3082.         if last:
  3083.             return self.tk.splitlist(self.tk.call(self._w, 'get', first, last))
  3084.         else:
  3085.             return self.tk.call(self._w, 'get', first)
  3086.  
  3087.     
  3088.     def index(self, index):
  3089.         '''Return index of item identified with INDEX.'''
  3090.         i = self.tk.call(self._w, 'index', index)
  3091.         if i == 'none':
  3092.             return None
  3093.         
  3094.         return getint(i)
  3095.  
  3096.     
  3097.     def insert(self, index, *elements):
  3098.         '''Insert ELEMENTS at INDEX.'''
  3099.         self.tk.call((self._w, 'insert', index) + elements)
  3100.  
  3101.     
  3102.     def nearest(self, y):
  3103.         '''Get index of item which is nearest to y coordinate Y.'''
  3104.         return getint(self.tk.call(self._w, 'nearest', y))
  3105.  
  3106.     
  3107.     def scan_mark(self, x, y):
  3108.         '''Remember the current X, Y coordinates.'''
  3109.         self.tk.call(self._w, 'scan', 'mark', x, y)
  3110.  
  3111.     
  3112.     def scan_dragto(self, x, y):
  3113.         '''Adjust the view of the listbox to 10 times the
  3114.         difference between X and Y and the coordinates given in
  3115.         scan_mark.'''
  3116.         self.tk.call(self._w, 'scan', 'dragto', x, y)
  3117.  
  3118.     
  3119.     def see(self, index):
  3120.         '''Scroll such that INDEX is visible.'''
  3121.         self.tk.call(self._w, 'see', index)
  3122.  
  3123.     
  3124.     def selection_anchor(self, index):
  3125.         '''Set the fixed end oft the selection to INDEX.'''
  3126.         self.tk.call(self._w, 'selection', 'anchor', index)
  3127.  
  3128.     select_anchor = selection_anchor
  3129.     
  3130.     def selection_clear(self, first, last = None):
  3131.         '''Clear the selection from FIRST to LAST (not included).'''
  3132.         self.tk.call(self._w, 'selection', 'clear', first, last)
  3133.  
  3134.     select_clear = selection_clear
  3135.     
  3136.     def selection_includes(self, index):
  3137.         '''Return 1 if INDEX is part of the selection.'''
  3138.         return self.tk.getboolean(self.tk.call(self._w, 'selection', 'includes', index))
  3139.  
  3140.     select_includes = selection_includes
  3141.     
  3142.     def selection_set(self, first, last = None):
  3143.         '''Set the selection from FIRST to LAST (not included) without
  3144.         changing the currently selected elements.'''
  3145.         self.tk.call(self._w, 'selection', 'set', first, last)
  3146.  
  3147.     select_set = selection_set
  3148.     
  3149.     def size(self):
  3150.         '''Return the number of elements in the listbox.'''
  3151.         return getint(self.tk.call(self._w, 'size'))
  3152.  
  3153.     
  3154.     def xview(self, *what):
  3155.         '''Query and change horizontal position of the view.'''
  3156.         if not what:
  3157.             return self._getdoubles(self.tk.call(self._w, 'xview'))
  3158.         
  3159.         self.tk.call((self._w, 'xview') + what)
  3160.  
  3161.     
  3162.     def xview_moveto(self, fraction):
  3163.         '''Adjust the view in the window so that FRACTION of the
  3164.         total width of the entry is off-screen to the left.'''
  3165.         self.tk.call(self._w, 'xview', 'moveto', fraction)
  3166.  
  3167.     
  3168.     def xview_scroll(self, number, what):
  3169.         '''Shift the x-view according to NUMBER which is measured in "units" or "pages" (WHAT).'''
  3170.         self.tk.call(self._w, 'xview', 'scroll', number, what)
  3171.  
  3172.     
  3173.     def yview(self, *what):
  3174.         '''Query and change vertical position of the view.'''
  3175.         if not what:
  3176.             return self._getdoubles(self.tk.call(self._w, 'yview'))
  3177.         
  3178.         self.tk.call((self._w, 'yview') + what)
  3179.  
  3180.     
  3181.     def yview_moveto(self, fraction):
  3182.         '''Adjust the view in the window so that FRACTION of the
  3183.         total width of the entry is off-screen to the top.'''
  3184.         self.tk.call(self._w, 'yview', 'moveto', fraction)
  3185.  
  3186.     
  3187.     def yview_scroll(self, number, what):
  3188.         '''Shift the y-view according to NUMBER which is measured in "units" or "pages" (WHAT).'''
  3189.         self.tk.call(self._w, 'yview', 'scroll', number, what)
  3190.  
  3191.     
  3192.     def itemcget(self, index, option):
  3193.         '''Return the resource value for an ITEM and an OPTION.'''
  3194.         return self.tk.call((self._w, 'itemcget') + (index, '-' + option))
  3195.  
  3196.     
  3197.     def itemconfigure(self, index, cnf = None, **kw):
  3198.         '''Configure resources of an ITEM.
  3199.  
  3200.         The values for resources are specified as keyword arguments.
  3201.         To get an overview about the allowed keyword arguments
  3202.         call the method without arguments.
  3203.         Valid resource names: background, bg, foreground, fg,
  3204.         selectbackground, selectforeground.'''
  3205.         return self._configure(('itemconfigure', index), cnf, kw)
  3206.  
  3207.     itemconfig = itemconfigure
  3208.  
  3209.  
  3210. class Menu(Widget):
  3211.     '''Menu widget which allows to display menu bars, pull-down menus and pop-up menus.'''
  3212.     
  3213.     def __init__(self, master = None, cnf = { }, **kw):
  3214.         '''Construct menu widget with the parent MASTER.
  3215.  
  3216.         Valid resource names: activebackground, activeborderwidth,
  3217.         activeforeground, background, bd, bg, borderwidth, cursor,
  3218.         disabledforeground, fg, font, foreground, postcommand, relief,
  3219.         selectcolor, takefocus, tearoff, tearoffcommand, title, type.'''
  3220.         Widget.__init__(self, master, 'menu', cnf, kw)
  3221.  
  3222.     
  3223.     def tk_bindForTraversal(self):
  3224.         pass
  3225.  
  3226.     
  3227.     def tk_mbPost(self):
  3228.         self.tk.call('tk_mbPost', self._w)
  3229.  
  3230.     
  3231.     def tk_mbUnpost(self):
  3232.         self.tk.call('tk_mbUnpost')
  3233.  
  3234.     
  3235.     def tk_traverseToMenu(self, char):
  3236.         self.tk.call('tk_traverseToMenu', self._w, char)
  3237.  
  3238.     
  3239.     def tk_traverseWithinMenu(self, char):
  3240.         self.tk.call('tk_traverseWithinMenu', self._w, char)
  3241.  
  3242.     
  3243.     def tk_getMenuButtons(self):
  3244.         return self.tk.call('tk_getMenuButtons', self._w)
  3245.  
  3246.     
  3247.     def tk_nextMenu(self, count):
  3248.         self.tk.call('tk_nextMenu', count)
  3249.  
  3250.     
  3251.     def tk_nextMenuEntry(self, count):
  3252.         self.tk.call('tk_nextMenuEntry', count)
  3253.  
  3254.     
  3255.     def tk_invokeMenu(self):
  3256.         self.tk.call('tk_invokeMenu', self._w)
  3257.  
  3258.     
  3259.     def tk_firstMenu(self):
  3260.         self.tk.call('tk_firstMenu', self._w)
  3261.  
  3262.     
  3263.     def tk_mbButtonDown(self):
  3264.         self.tk.call('tk_mbButtonDown', self._w)
  3265.  
  3266.     
  3267.     def tk_popup(self, x, y, entry = ''):
  3268.         '''Post the menu at position X,Y with entry ENTRY.'''
  3269.         self.tk.call('tk_popup', self._w, x, y, entry)
  3270.  
  3271.     
  3272.     def activate(self, index):
  3273.         '''Activate entry at INDEX.'''
  3274.         self.tk.call(self._w, 'activate', index)
  3275.  
  3276.     
  3277.     def add(self, itemType, cnf = { }, **kw):
  3278.         '''Internal function.'''
  3279.         self.tk.call((self._w, 'add', itemType) + self._options(cnf, kw))
  3280.  
  3281.     
  3282.     def add_cascade(self, cnf = { }, **kw):
  3283.         '''Add hierarchical menu item.'''
  3284.         if not cnf:
  3285.             pass
  3286.         self.add('cascade', kw)
  3287.  
  3288.     
  3289.     def add_checkbutton(self, cnf = { }, **kw):
  3290.         '''Add checkbutton menu item.'''
  3291.         if not cnf:
  3292.             pass
  3293.         self.add('checkbutton', kw)
  3294.  
  3295.     
  3296.     def add_command(self, cnf = { }, **kw):
  3297.         '''Add command menu item.'''
  3298.         if not cnf:
  3299.             pass
  3300.         self.add('command', kw)
  3301.  
  3302.     
  3303.     def add_radiobutton(self, cnf = { }, **kw):
  3304.         '''Addd radio menu item.'''
  3305.         if not cnf:
  3306.             pass
  3307.         self.add('radiobutton', kw)
  3308.  
  3309.     
  3310.     def add_separator(self, cnf = { }, **kw):
  3311.         '''Add separator.'''
  3312.         if not cnf:
  3313.             pass
  3314.         self.add('separator', kw)
  3315.  
  3316.     
  3317.     def insert(self, index, itemType, cnf = { }, **kw):
  3318.         '''Internal function.'''
  3319.         self.tk.call((self._w, 'insert', index, itemType) + self._options(cnf, kw))
  3320.  
  3321.     
  3322.     def insert_cascade(self, index, cnf = { }, **kw):
  3323.         '''Add hierarchical menu item at INDEX.'''
  3324.         if not cnf:
  3325.             pass
  3326.         self.insert(index, 'cascade', kw)
  3327.  
  3328.     
  3329.     def insert_checkbutton(self, index, cnf = { }, **kw):
  3330.         '''Add checkbutton menu item at INDEX.'''
  3331.         if not cnf:
  3332.             pass
  3333.         self.insert(index, 'checkbutton', kw)
  3334.  
  3335.     
  3336.     def insert_command(self, index, cnf = { }, **kw):
  3337.         '''Add command menu item at INDEX.'''
  3338.         if not cnf:
  3339.             pass
  3340.         self.insert(index, 'command', kw)
  3341.  
  3342.     
  3343.     def insert_radiobutton(self, index, cnf = { }, **kw):
  3344.         '''Addd radio menu item at INDEX.'''
  3345.         if not cnf:
  3346.             pass
  3347.         self.insert(index, 'radiobutton', kw)
  3348.  
  3349.     
  3350.     def insert_separator(self, index, cnf = { }, **kw):
  3351.         '''Add separator at INDEX.'''
  3352.         if not cnf:
  3353.             pass
  3354.         self.insert(index, 'separator', kw)
  3355.  
  3356.     
  3357.     def delete(self, index1, index2 = None):
  3358.         '''Delete menu items between INDEX1 and INDEX2 (not included).'''
  3359.         self.tk.call(self._w, 'delete', index1, index2)
  3360.  
  3361.     
  3362.     def entrycget(self, index, option):
  3363.         '''Return the resource value of an menu item for OPTION at INDEX.'''
  3364.         return self.tk.call(self._w, 'entrycget', index, '-' + option)
  3365.  
  3366.     
  3367.     def entryconfigure(self, index, cnf = None, **kw):
  3368.         '''Configure a menu item at INDEX.'''
  3369.         return self._configure(('entryconfigure', index), cnf, kw)
  3370.  
  3371.     entryconfig = entryconfigure
  3372.     
  3373.     def index(self, index):
  3374.         '''Return the index of a menu item identified by INDEX.'''
  3375.         i = self.tk.call(self._w, 'index', index)
  3376.         if i == 'none':
  3377.             return None
  3378.         
  3379.         return getint(i)
  3380.  
  3381.     
  3382.     def invoke(self, index):
  3383.         '''Invoke a menu item identified by INDEX and execute
  3384.         the associated command.'''
  3385.         return self.tk.call(self._w, 'invoke', index)
  3386.  
  3387.     
  3388.     def post(self, x, y):
  3389.         '''Display a menu at position X,Y.'''
  3390.         self.tk.call(self._w, 'post', x, y)
  3391.  
  3392.     
  3393.     def type(self, index):
  3394.         '''Return the type of the menu item at INDEX.'''
  3395.         return self.tk.call(self._w, 'type', index)
  3396.  
  3397.     
  3398.     def unpost(self):
  3399.         '''Unmap a menu.'''
  3400.         self.tk.call(self._w, 'unpost')
  3401.  
  3402.     
  3403.     def yposition(self, index):
  3404.         '''Return the y-position of the topmost pixel of the menu item at INDEX.'''
  3405.         return getint(self.tk.call(self._w, 'yposition', index))
  3406.  
  3407.  
  3408.  
  3409. class Menubutton(Widget):
  3410.     '''Menubutton widget, obsolete since Tk8.0.'''
  3411.     
  3412.     def __init__(self, master = None, cnf = { }, **kw):
  3413.         Widget.__init__(self, master, 'menubutton', cnf, kw)
  3414.  
  3415.  
  3416.  
  3417. class Message(Widget):
  3418.     '''Message widget to display multiline text. Obsolete since Label does it too.'''
  3419.     
  3420.     def __init__(self, master = None, cnf = { }, **kw):
  3421.         Widget.__init__(self, master, 'message', cnf, kw)
  3422.  
  3423.  
  3424.  
  3425. class Radiobutton(Widget):
  3426.     '''Radiobutton widget which shows only one of several buttons in on-state.'''
  3427.     
  3428.     def __init__(self, master = None, cnf = { }, **kw):
  3429.         '''Construct a radiobutton widget with the parent MASTER.
  3430.  
  3431.         Valid resource names: activebackground, activeforeground, anchor,
  3432.         background, bd, bg, bitmap, borderwidth, command, cursor,
  3433.         disabledforeground, fg, font, foreground, height,
  3434.         highlightbackground, highlightcolor, highlightthickness, image,
  3435.         indicatoron, justify, padx, pady, relief, selectcolor, selectimage,
  3436.         state, takefocus, text, textvariable, underline, value, variable,
  3437.         width, wraplength.'''
  3438.         Widget.__init__(self, master, 'radiobutton', cnf, kw)
  3439.  
  3440.     
  3441.     def deselect(self):
  3442.         '''Put the button in off-state.'''
  3443.         self.tk.call(self._w, 'deselect')
  3444.  
  3445.     
  3446.     def flash(self):
  3447.         '''Flash the button.'''
  3448.         self.tk.call(self._w, 'flash')
  3449.  
  3450.     
  3451.     def invoke(self):
  3452.         '''Toggle the button and invoke a command if given as resource.'''
  3453.         return self.tk.call(self._w, 'invoke')
  3454.  
  3455.     
  3456.     def select(self):
  3457.         '''Put the button in on-state.'''
  3458.         self.tk.call(self._w, 'select')
  3459.  
  3460.  
  3461.  
  3462. class Scale(Widget):
  3463.     '''Scale widget which can display a numerical scale.'''
  3464.     
  3465.     def __init__(self, master = None, cnf = { }, **kw):
  3466.         '''Construct a scale widget with the parent MASTER.
  3467.  
  3468.         Valid resource names: activebackground, background, bigincrement, bd,
  3469.         bg, borderwidth, command, cursor, digits, fg, font, foreground, from,
  3470.         highlightbackground, highlightcolor, highlightthickness, label,
  3471.         length, orient, relief, repeatdelay, repeatinterval, resolution,
  3472.         showvalue, sliderlength, sliderrelief, state, takefocus,
  3473.         tickinterval, to, troughcolor, variable, width.'''
  3474.         Widget.__init__(self, master, 'scale', cnf, kw)
  3475.  
  3476.     
  3477.     def get(self):
  3478.         '''Get the current value as integer or float.'''
  3479.         value = self.tk.call(self._w, 'get')
  3480.         
  3481.         try:
  3482.             return getint(value)
  3483.         except ValueError:
  3484.             return getdouble(value)
  3485.  
  3486.  
  3487.     
  3488.     def set(self, value):
  3489.         '''Set the value to VALUE.'''
  3490.         self.tk.call(self._w, 'set', value)
  3491.  
  3492.     
  3493.     def coords(self, value = None):
  3494.         '''Return a tuple (X,Y) of the point along the centerline of the
  3495.         trough that corresponds to VALUE or the current value if None is
  3496.         given.'''
  3497.         return self._getints(self.tk.call(self._w, 'coords', value))
  3498.  
  3499.     
  3500.     def identify(self, x, y):
  3501.         '''Return where the point X,Y lies. Valid return values are "slider",
  3502.         "though1" and "though2".'''
  3503.         return self.tk.call(self._w, 'identify', x, y)
  3504.  
  3505.  
  3506.  
  3507. class Scrollbar(Widget):
  3508.     '''Scrollbar widget which displays a slider at a certain position.'''
  3509.     
  3510.     def __init__(self, master = None, cnf = { }, **kw):
  3511.         '''Construct a scrollbar widget with the parent MASTER.
  3512.  
  3513.         Valid resource names: activebackground, activerelief,
  3514.         background, bd, bg, borderwidth, command, cursor,
  3515.         elementborderwidth, highlightbackground,
  3516.         highlightcolor, highlightthickness, jump, orient,
  3517.         relief, repeatdelay, repeatinterval, takefocus,
  3518.         troughcolor, width.'''
  3519.         Widget.__init__(self, master, 'scrollbar', cnf, kw)
  3520.  
  3521.     
  3522.     def activate(self, index):
  3523.         '''Display the element at INDEX with activebackground and activerelief.
  3524.         INDEX can be "arrow1","slider" or "arrow2".'''
  3525.         self.tk.call(self._w, 'activate', index)
  3526.  
  3527.     
  3528.     def delta(self, deltax, deltay):
  3529.         '''Return the fractional change of the scrollbar setting if it
  3530.         would be moved by DELTAX or DELTAY pixels.'''
  3531.         return getdouble(self.tk.call(self._w, 'delta', deltax, deltay))
  3532.  
  3533.     
  3534.     def fraction(self, x, y):
  3535.         '''Return the fractional value which corresponds to a slider
  3536.         position of X,Y.'''
  3537.         return getdouble(self.tk.call(self._w, 'fraction', x, y))
  3538.  
  3539.     
  3540.     def identify(self, x, y):
  3541.         '''Return the element under position X,Y as one of
  3542.         "arrow1","slider","arrow2" or "".'''
  3543.         return self.tk.call(self._w, 'identify', x, y)
  3544.  
  3545.     
  3546.     def get(self):
  3547.         '''Return the current fractional values (upper and lower end)
  3548.         of the slider position.'''
  3549.         return self._getdoubles(self.tk.call(self._w, 'get'))
  3550.  
  3551.     
  3552.     def set(self, *args):
  3553.         '''Set the fractional values of the slider position (upper and
  3554.         lower ends as value between 0 and 1).'''
  3555.         self.tk.call((self._w, 'set') + args)
  3556.  
  3557.  
  3558.  
  3559. class Text(Widget):
  3560.     '''Text widget which can display text in various forms.'''
  3561.     
  3562.     def __init__(self, master = None, cnf = { }, **kw):
  3563.         '''Construct a text widget with the parent MASTER.
  3564.  
  3565.         STANDARD OPTIONS
  3566.  
  3567.             background, borderwidth, cursor,
  3568.             exportselection, font, foreground,
  3569.             highlightbackground, highlightcolor,
  3570.             highlightthickness, insertbackground,
  3571.             insertborderwidth, insertofftime,
  3572.             insertontime, insertwidth, padx, pady,
  3573.             relief, selectbackground,
  3574.             selectborderwidth, selectforeground,
  3575.             setgrid, takefocus,
  3576.             xscrollcommand, yscrollcommand,
  3577.  
  3578.         WIDGET-SPECIFIC OPTIONS
  3579.  
  3580.             autoseparators, height, maxundo,
  3581.             spacing1, spacing2, spacing3,
  3582.             state, tabs, undo, width, wrap,
  3583.  
  3584.         '''
  3585.         Widget.__init__(self, master, 'text', cnf, kw)
  3586.  
  3587.     
  3588.     def bbox(self, *args):
  3589.         '''Return a tuple of (x,y,width,height) which gives the bounding
  3590.         box of the visible part of the character at the index in ARGS.'''
  3591.         if not self._getints(self.tk.call((self._w, 'bbox') + args)):
  3592.             pass
  3593.  
  3594.     
  3595.     def tk_textSelectTo(self, index):
  3596.         self.tk.call('tk_textSelectTo', self._w, index)
  3597.  
  3598.     
  3599.     def tk_textBackspace(self):
  3600.         self.tk.call('tk_textBackspace', self._w)
  3601.  
  3602.     
  3603.     def tk_textIndexCloser(self, a, b, c):
  3604.         self.tk.call('tk_textIndexCloser', self._w, a, b, c)
  3605.  
  3606.     
  3607.     def tk_textResetAnchor(self, index):
  3608.         self.tk.call('tk_textResetAnchor', self._w, index)
  3609.  
  3610.     
  3611.     def compare(self, index1, op, index2):
  3612.         '''Return whether between index INDEX1 and index INDEX2 the
  3613.         relation OP is satisfied. OP is one of <, <=, ==, >=, >, or !=.'''
  3614.         return self.tk.getboolean(self.tk.call(self._w, 'compare', index1, op, index2))
  3615.  
  3616.     
  3617.     def debug(self, boolean = None):
  3618.         '''Turn on the internal consistency checks of the B-Tree inside the text
  3619.         widget according to BOOLEAN.'''
  3620.         return self.tk.getboolean(self.tk.call(self._w, 'debug', boolean))
  3621.  
  3622.     
  3623.     def delete(self, index1, index2 = None):
  3624.         '''Delete the characters between INDEX1 and INDEX2 (not included).'''
  3625.         self.tk.call(self._w, 'delete', index1, index2)
  3626.  
  3627.     
  3628.     def dlineinfo(self, index):
  3629.         '''Return tuple (x,y,width,height,baseline) giving the bounding box
  3630.         and baseline position of the visible part of the line containing
  3631.         the character at INDEX.'''
  3632.         return self._getints(self.tk.call(self._w, 'dlineinfo', index))
  3633.  
  3634.     
  3635.     def dump(self, index1, index2 = None, command = None, **kw):
  3636.         """Return the contents of the widget between index1 and index2.
  3637.  
  3638.         The type of contents returned in filtered based on the keyword
  3639.         parameters; if 'all', 'image', 'mark', 'tag', 'text', or 'window' are
  3640.         given and true, then the corresponding items are returned. The result
  3641.         is a list of triples of the form (key, value, index). If none of the
  3642.         keywords are true then 'all' is used by default.
  3643.  
  3644.         If the 'command' argument is given, it is called once for each element
  3645.         of the list of triples, with the values of each triple serving as the
  3646.         arguments to the function. In this case the list is not returned."""
  3647.         args = []
  3648.         func_name = None
  3649.         result = None
  3650.         if not command:
  3651.             result = []
  3652.             
  3653.             def append_triple(key, value, index, result = result):
  3654.                 result.append((key, value, index))
  3655.  
  3656.             command = append_triple
  3657.         
  3658.         
  3659.         try:
  3660.             if not isinstance(command, str):
  3661.                 func_name = command = self._register(command)
  3662.             
  3663.             args += [
  3664.                 '-command',
  3665.                 command]
  3666.             for key in kw:
  3667.                 if kw[key]:
  3668.                     args.append('-' + key)
  3669.                     continue
  3670.             
  3671.             args.append(index1)
  3672.             if index2:
  3673.                 args.append(index2)
  3674.             
  3675.             self.tk.call(self._w, 'dump', *args)
  3676.             return result
  3677.         finally:
  3678.             if func_name:
  3679.                 self.deletecommand(func_name)
  3680.             
  3681.  
  3682.  
  3683.     
  3684.     def edit(self, *args):
  3685.         '''Internal method
  3686.  
  3687.         This method controls the undo mechanism and
  3688.         the modified flag. The exact behavior of the
  3689.         command depends on the option argument that
  3690.         follows the edit argument. The following forms
  3691.         of the command are currently supported:
  3692.  
  3693.         edit_modified, edit_redo, edit_reset, edit_separator
  3694.         and edit_undo
  3695.  
  3696.         '''
  3697.         if not self._getints(self.tk.call((self._w, 'edit') + args)):
  3698.             pass
  3699.         return ()
  3700.  
  3701.     
  3702.     def edit_modified(self, arg = None):
  3703.         '''Get or Set the modified flag
  3704.  
  3705.         If arg is not specified, returns the modified
  3706.         flag of the widget. The insert, delete, edit undo and
  3707.         edit redo commands or the user can set or clear the
  3708.         modified flag. If boolean is specified, sets the
  3709.         modified flag of the widget to arg.
  3710.         '''
  3711.         return self.edit('modified', arg)
  3712.  
  3713.     
  3714.     def edit_redo(self):
  3715.         '''Redo the last undone edit
  3716.  
  3717.         When the undo option is true, reapplies the last
  3718.         undone edits provided no other edits were done since
  3719.         then. Generates an error when the redo stack is empty.
  3720.         Does nothing when the undo option is false.
  3721.         '''
  3722.         return self.edit('redo')
  3723.  
  3724.     
  3725.     def edit_reset(self):
  3726.         '''Clears the undo and redo stacks
  3727.         '''
  3728.         return self.edit('reset')
  3729.  
  3730.     
  3731.     def edit_separator(self):
  3732.         '''Inserts a separator (boundary) on the undo stack.
  3733.  
  3734.         Does nothing when the undo option is false
  3735.         '''
  3736.         return self.edit('separator')
  3737.  
  3738.     
  3739.     def edit_undo(self):
  3740.         '''Undoes the last edit action
  3741.  
  3742.         If the undo option is true. An edit action is defined
  3743.         as all the insert and delete commands that are recorded
  3744.         on the undo stack in between two separators. Generates
  3745.         an error when the undo stack is empty. Does nothing
  3746.         when the undo option is false
  3747.         '''
  3748.         return self.edit('undo')
  3749.  
  3750.     
  3751.     def get(self, index1, index2 = None):
  3752.         '''Return the text from INDEX1 to INDEX2 (not included).'''
  3753.         return self.tk.call(self._w, 'get', index1, index2)
  3754.  
  3755.     
  3756.     def image_cget(self, index, option):
  3757.         '''Return the value of OPTION of an embedded image at INDEX.'''
  3758.         if option[:1] != '-':
  3759.             option = '-' + option
  3760.         
  3761.         if option[-1:] == '_':
  3762.             option = option[:-1]
  3763.         
  3764.         return self.tk.call(self._w, 'image', 'cget', index, option)
  3765.  
  3766.     
  3767.     def image_configure(self, index, cnf = None, **kw):
  3768.         '''Configure an embedded image at INDEX.'''
  3769.         return self._configure(('image', 'configure', index), cnf, kw)
  3770.  
  3771.     
  3772.     def image_create(self, index, cnf = { }, **kw):
  3773.         '''Create an embedded image at INDEX.'''
  3774.         return self.tk.call(self._w, 'image', 'create', index, *self._options(cnf, kw))
  3775.  
  3776.     
  3777.     def image_names(self):
  3778.         '''Return all names of embedded images in this widget.'''
  3779.         return self.tk.call(self._w, 'image', 'names')
  3780.  
  3781.     
  3782.     def index(self, index):
  3783.         '''Return the index in the form line.char for INDEX.'''
  3784.         return self.tk.call(self._w, 'index', index)
  3785.  
  3786.     
  3787.     def insert(self, index, chars, *args):
  3788.         '''Insert CHARS before the characters at INDEX. An additional
  3789.         tag can be given in ARGS. Additional CHARS and tags can follow in ARGS.'''
  3790.         self.tk.call((self._w, 'insert', index, chars) + args)
  3791.  
  3792.     
  3793.     def mark_gravity(self, markName, direction = None):
  3794.         '''Change the gravity of a mark MARKNAME to DIRECTION (LEFT or RIGHT).
  3795.         Return the current value if None is given for DIRECTION.'''
  3796.         return self.tk.call((self._w, 'mark', 'gravity', markName, direction))
  3797.  
  3798.     
  3799.     def mark_names(self):
  3800.         '''Return all mark names.'''
  3801.         return self.tk.splitlist(self.tk.call(self._w, 'mark', 'names'))
  3802.  
  3803.     
  3804.     def mark_set(self, markName, index):
  3805.         '''Set mark MARKNAME before the character at INDEX.'''
  3806.         self.tk.call(self._w, 'mark', 'set', markName, index)
  3807.  
  3808.     
  3809.     def mark_unset(self, *markNames):
  3810.         '''Delete all marks in MARKNAMES.'''
  3811.         self.tk.call((self._w, 'mark', 'unset') + markNames)
  3812.  
  3813.     
  3814.     def mark_next(self, index):
  3815.         '''Return the name of the next mark after INDEX.'''
  3816.         if not self.tk.call(self._w, 'mark', 'next', index):
  3817.             pass
  3818.  
  3819.     
  3820.     def mark_previous(self, index):
  3821.         '''Return the name of the previous mark before INDEX.'''
  3822.         if not self.tk.call(self._w, 'mark', 'previous', index):
  3823.             pass
  3824.  
  3825.     
  3826.     def scan_mark(self, x, y):
  3827.         '''Remember the current X, Y coordinates.'''
  3828.         self.tk.call(self._w, 'scan', 'mark', x, y)
  3829.  
  3830.     
  3831.     def scan_dragto(self, x, y):
  3832.         '''Adjust the view of the text to 10 times the
  3833.         difference between X and Y and the coordinates given in
  3834.         scan_mark.'''
  3835.         self.tk.call(self._w, 'scan', 'dragto', x, y)
  3836.  
  3837.     
  3838.     def search(self, pattern, index, stopindex = None, forwards = None, backwards = None, exact = None, regexp = None, nocase = None, count = None):
  3839.         '''Search PATTERN beginning from INDEX until STOPINDEX.
  3840.         Return the index of the first character of a match or an empty string.'''
  3841.         args = [
  3842.             self._w,
  3843.             'search']
  3844.         if forwards:
  3845.             args.append('-forwards')
  3846.         
  3847.         if backwards:
  3848.             args.append('-backwards')
  3849.         
  3850.         if exact:
  3851.             args.append('-exact')
  3852.         
  3853.         if regexp:
  3854.             args.append('-regexp')
  3855.         
  3856.         if nocase:
  3857.             args.append('-nocase')
  3858.         
  3859.         if count:
  3860.             args.append('-count')
  3861.             args.append(count)
  3862.         
  3863.         if pattern[0] == '-':
  3864.             args.append('--')
  3865.         
  3866.         args.append(pattern)
  3867.         args.append(index)
  3868.         if stopindex:
  3869.             args.append(stopindex)
  3870.         
  3871.         return self.tk.call(tuple(args))
  3872.  
  3873.     
  3874.     def see(self, index):
  3875.         '''Scroll such that the character at INDEX is visible.'''
  3876.         self.tk.call(self._w, 'see', index)
  3877.  
  3878.     
  3879.     def tag_add(self, tagName, index1, *args):
  3880.         '''Add tag TAGNAME to all characters between INDEX1 and index2 in ARGS.
  3881.         Additional pairs of indices may follow in ARGS.'''
  3882.         self.tk.call((self._w, 'tag', 'add', tagName, index1) + args)
  3883.  
  3884.     
  3885.     def tag_unbind(self, tagName, sequence, funcid = None):
  3886.         '''Unbind for all characters with TAGNAME for event SEQUENCE  the
  3887.         function identified with FUNCID.'''
  3888.         self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
  3889.         if funcid:
  3890.             self.deletecommand(funcid)
  3891.         
  3892.  
  3893.     
  3894.     def tag_bind(self, tagName, sequence, func, add = None):
  3895.         '''Bind to all characters with TAGNAME at event SEQUENCE a call to function FUNC.
  3896.  
  3897.         An additional boolean parameter ADD specifies whether FUNC will be
  3898.         called additionally to the other bound function or whether it will
  3899.         replace the previous function. See bind for the return value.'''
  3900.         return self._bind((self._w, 'tag', 'bind', tagName), sequence, func, add)
  3901.  
  3902.     
  3903.     def tag_cget(self, tagName, option):
  3904.         '''Return the value of OPTION for tag TAGNAME.'''
  3905.         if option[:1] != '-':
  3906.             option = '-' + option
  3907.         
  3908.         if option[-1:] == '_':
  3909.             option = option[:-1]
  3910.         
  3911.         return self.tk.call(self._w, 'tag', 'cget', tagName, option)
  3912.  
  3913.     
  3914.     def tag_configure(self, tagName, cnf = None, **kw):
  3915.         '''Configure a tag TAGNAME.'''
  3916.         return self._configure(('tag', 'configure', tagName), cnf, kw)
  3917.  
  3918.     tag_config = tag_configure
  3919.     
  3920.     def tag_delete(self, *tagNames):
  3921.         '''Delete all tags in TAGNAMES.'''
  3922.         self.tk.call((self._w, 'tag', 'delete') + tagNames)
  3923.  
  3924.     
  3925.     def tag_lower(self, tagName, belowThis = None):
  3926.         '''Change the priority of tag TAGNAME such that it is lower
  3927.         than the priority of BELOWTHIS.'''
  3928.         self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
  3929.  
  3930.     
  3931.     def tag_names(self, index = None):
  3932.         '''Return a list of all tag names.'''
  3933.         return self.tk.splitlist(self.tk.call(self._w, 'tag', 'names', index))
  3934.  
  3935.     
  3936.     def tag_nextrange(self, tagName, index1, index2 = None):
  3937.         '''Return a list of start and end index for the first sequence of
  3938.         characters between INDEX1 and INDEX2 which all have tag TAGNAME.
  3939.         The text is searched forward from INDEX1.'''
  3940.         return self.tk.splitlist(self.tk.call(self._w, 'tag', 'nextrange', tagName, index1, index2))
  3941.  
  3942.     
  3943.     def tag_prevrange(self, tagName, index1, index2 = None):
  3944.         '''Return a list of start and end index for the first sequence of
  3945.         characters between INDEX1 and INDEX2 which all have tag TAGNAME.
  3946.         The text is searched backwards from INDEX1.'''
  3947.         return self.tk.splitlist(self.tk.call(self._w, 'tag', 'prevrange', tagName, index1, index2))
  3948.  
  3949.     
  3950.     def tag_raise(self, tagName, aboveThis = None):
  3951.         '''Change the priority of tag TAGNAME such that it is higher
  3952.         than the priority of ABOVETHIS.'''
  3953.         self.tk.call(self._w, 'tag', 'raise', tagName, aboveThis)
  3954.  
  3955.     
  3956.     def tag_ranges(self, tagName):
  3957.         '''Return a list of ranges of text which have tag TAGNAME.'''
  3958.         return self.tk.splitlist(self.tk.call(self._w, 'tag', 'ranges', tagName))
  3959.  
  3960.     
  3961.     def tag_remove(self, tagName, index1, index2 = None):
  3962.         '''Remove tag TAGNAME from all characters between INDEX1 and INDEX2.'''
  3963.         self.tk.call(self._w, 'tag', 'remove', tagName, index1, index2)
  3964.  
  3965.     
  3966.     def window_cget(self, index, option):
  3967.         '''Return the value of OPTION of an embedded window at INDEX.'''
  3968.         if option[:1] != '-':
  3969.             option = '-' + option
  3970.         
  3971.         if option[-1:] == '_':
  3972.             option = option[:-1]
  3973.         
  3974.         return self.tk.call(self._w, 'window', 'cget', index, option)
  3975.  
  3976.     
  3977.     def window_configure(self, index, cnf = None, **kw):
  3978.         '''Configure an embedded window at INDEX.'''
  3979.         return self._configure(('window', 'configure', index), cnf, kw)
  3980.  
  3981.     window_config = window_configure
  3982.     
  3983.     def window_create(self, index, cnf = { }, **kw):
  3984.         '''Create a window at INDEX.'''
  3985.         self.tk.call((self._w, 'window', 'create', index) + self._options(cnf, kw))
  3986.  
  3987.     
  3988.     def window_names(self):
  3989.         '''Return all names of embedded windows in this widget.'''
  3990.         return self.tk.splitlist(self.tk.call(self._w, 'window', 'names'))
  3991.  
  3992.     
  3993.     def xview(self, *what):
  3994.         '''Query and change horizontal position of the view.'''
  3995.         if not what:
  3996.             return self._getdoubles(self.tk.call(self._w, 'xview'))
  3997.         
  3998.         self.tk.call((self._w, 'xview') + what)
  3999.  
  4000.     
  4001.     def xview_moveto(self, fraction):
  4002.         '''Adjusts the view in the window so that FRACTION of the
  4003.         total width of the canvas is off-screen to the left.'''
  4004.         self.tk.call(self._w, 'xview', 'moveto', fraction)
  4005.  
  4006.     
  4007.     def xview_scroll(self, number, what):
  4008.         '''Shift the x-view according to NUMBER which is measured
  4009.         in "units" or "pages" (WHAT).'''
  4010.         self.tk.call(self._w, 'xview', 'scroll', number, what)
  4011.  
  4012.     
  4013.     def yview(self, *what):
  4014.         '''Query and change vertical position of the view.'''
  4015.         if not what:
  4016.             return self._getdoubles(self.tk.call(self._w, 'yview'))
  4017.         
  4018.         self.tk.call((self._w, 'yview') + what)
  4019.  
  4020.     
  4021.     def yview_moveto(self, fraction):
  4022.         '''Adjusts the view in the window so that FRACTION of the
  4023.         total height of the canvas is off-screen to the top.'''
  4024.         self.tk.call(self._w, 'yview', 'moveto', fraction)
  4025.  
  4026.     
  4027.     def yview_scroll(self, number, what):
  4028.         '''Shift the y-view according to NUMBER which is measured
  4029.         in "units" or "pages" (WHAT).'''
  4030.         self.tk.call(self._w, 'yview', 'scroll', number, what)
  4031.  
  4032.     
  4033.     def yview_pickplace(self, *what):
  4034.         '''Obsolete function, use see.'''
  4035.         self.tk.call((self._w, 'yview', '-pickplace') + what)
  4036.  
  4037.  
  4038.  
  4039. class _setit:
  4040.     '''Internal class. It wraps the command in the widget OptionMenu.'''
  4041.     
  4042.     def __init__(self, var, value, callback = None):
  4043.         self._setit__value = value
  4044.         self._setit__var = var
  4045.         self._setit__callback = callback
  4046.  
  4047.     
  4048.     def __call__(self, *args):
  4049.         self._setit__var.set(self._setit__value)
  4050.         if self._setit__callback:
  4051.             self._setit__callback(self._setit__value, *args)
  4052.         
  4053.  
  4054.  
  4055.  
  4056. class OptionMenu(Menubutton):
  4057.     '''OptionMenu which allows the user to select a value from a menu.'''
  4058.     
  4059.     def __init__(self, master, variable, value, *values, **kwargs):
  4060.         '''Construct an optionmenu widget with the parent MASTER, with
  4061.         the resource textvariable set to VARIABLE, the initially selected
  4062.         value VALUE, the other menu values VALUES and an additional
  4063.         keyword argument command.'''
  4064.         kw = {
  4065.             'borderwidth': 2,
  4066.             'textvariable': variable,
  4067.             'indicatoron': 1,
  4068.             'relief': RAISED,
  4069.             'anchor': 'c',
  4070.             'highlightthickness': 2 }
  4071.         Widget.__init__(self, master, 'menubutton', kw)
  4072.         self.widgetName = 'tk_optionMenu'
  4073.         menu = self._OptionMenu__menu = Menu(self, name = 'menu', tearoff = 0)
  4074.         self.menuname = menu._w
  4075.         callback = kwargs.get('command')
  4076.         if kwargs.has_key('command'):
  4077.             del kwargs['command']
  4078.         
  4079.         if kwargs:
  4080.             raise TclError, 'unknown option -' + kwargs.keys()[0]
  4081.         
  4082.         menu.add_command(label = value, command = _setit(variable, value, callback))
  4083.         for v in values:
  4084.             menu.add_command(label = v, command = _setit(variable, v, callback))
  4085.         
  4086.         self['menu'] = menu
  4087.  
  4088.     
  4089.     def __getitem__(self, name):
  4090.         if name == 'menu':
  4091.             return self._OptionMenu__menu
  4092.         
  4093.         return Widget.__getitem__(self, name)
  4094.  
  4095.     
  4096.     def destroy(self):
  4097.         '''Destroy this widget and the associated menu.'''
  4098.         Menubutton.destroy(self)
  4099.         self._OptionMenu__menu = None
  4100.  
  4101.  
  4102.  
  4103. class Image:
  4104.     '''Base class for images.'''
  4105.     _last_id = 0
  4106.     
  4107.     def __init__(self, imgtype, name = None, cnf = { }, master = None, **kw):
  4108.         self.name = None
  4109.         if not master:
  4110.             master = _default_root
  4111.             if not master:
  4112.                 raise RuntimeError, 'Too early to create image'
  4113.             
  4114.         
  4115.         self.tk = master.tk
  4116.         if not name:
  4117.             Image._last_id += 1
  4118.             name = 'pyimage%r' % (Image._last_id,)
  4119.             if name[0] == '-':
  4120.                 name = '_' + name[1:]
  4121.             
  4122.         
  4123.         if kw and cnf:
  4124.             cnf = _cnfmerge((cnf, kw))
  4125.         elif kw:
  4126.             cnf = kw
  4127.         
  4128.         options = ()
  4129.         for k, v in cnf.items():
  4130.             if callable(v):
  4131.                 v = self._register(v)
  4132.             
  4133.             options = options + ('-' + k, v)
  4134.         
  4135.         self.tk.call(('image', 'create', imgtype, name) + options)
  4136.         self.name = name
  4137.  
  4138.     
  4139.     def __str__(self):
  4140.         return self.name
  4141.  
  4142.     
  4143.     def __del__(self):
  4144.         if self.name:
  4145.             
  4146.             try:
  4147.                 self.tk.call('image', 'delete', self.name)
  4148.             except TclError:
  4149.                 pass
  4150.             except:
  4151.                 None<EXCEPTION MATCH>TclError
  4152.             
  4153.  
  4154.         None<EXCEPTION MATCH>TclError
  4155.  
  4156.     
  4157.     def __setitem__(self, key, value):
  4158.         self.tk.call(self.name, 'configure', '-' + key, value)
  4159.  
  4160.     
  4161.     def __getitem__(self, key):
  4162.         return self.tk.call(self.name, 'configure', '-' + key)
  4163.  
  4164.     
  4165.     def configure(self, **kw):
  4166.         '''Configure the image.'''
  4167.         res = ()
  4168.         for k, v in _cnfmerge(kw).items():
  4169.             if v is not None:
  4170.                 if k[-1] == '_':
  4171.                     k = k[:-1]
  4172.                 
  4173.                 if callable(v):
  4174.                     v = self._register(v)
  4175.                 
  4176.                 res = res + ('-' + k, v)
  4177.                 continue
  4178.         
  4179.         self.tk.call((self.name, 'config') + res)
  4180.  
  4181.     config = configure
  4182.     
  4183.     def height(self):
  4184.         '''Return the height of the image.'''
  4185.         return getint(self.tk.call('image', 'height', self.name))
  4186.  
  4187.     
  4188.     def type(self):
  4189.         '''Return the type of the imgage, e.g. "photo" or "bitmap".'''
  4190.         return self.tk.call('image', 'type', self.name)
  4191.  
  4192.     
  4193.     def width(self):
  4194.         '''Return the width of the image.'''
  4195.         return getint(self.tk.call('image', 'width', self.name))
  4196.  
  4197.  
  4198.  
  4199. class PhotoImage(Image):
  4200.     '''Widget which can display colored images in GIF, PPM/PGM format.'''
  4201.     
  4202.     def __init__(self, name = None, cnf = { }, master = None, **kw):
  4203.         '''Create an image with NAME.
  4204.  
  4205.         Valid resource names: data, format, file, gamma, height, palette,
  4206.         width.'''
  4207.         Image.__init__(self, 'photo', name, cnf, master, **kw)
  4208.  
  4209.     
  4210.     def blank(self):
  4211.         '''Display a transparent image.'''
  4212.         self.tk.call(self.name, 'blank')
  4213.  
  4214.     
  4215.     def cget(self, option):
  4216.         '''Return the value of OPTION.'''
  4217.         return self.tk.call(self.name, 'cget', '-' + option)
  4218.  
  4219.     
  4220.     def __getitem__(self, key):
  4221.         return self.tk.call(self.name, 'cget', '-' + key)
  4222.  
  4223.     
  4224.     def copy(self):
  4225.         '''Return a new PhotoImage with the same image as this widget.'''
  4226.         destImage = PhotoImage()
  4227.         self.tk.call(destImage, 'copy', self.name)
  4228.         return destImage
  4229.  
  4230.     
  4231.     def zoom(self, x, y = ''):
  4232.         '''Return a new PhotoImage with the same image as this widget
  4233.         but zoom it with X and Y.'''
  4234.         destImage = PhotoImage()
  4235.         if y == '':
  4236.             y = x
  4237.         
  4238.         self.tk.call(destImage, 'copy', self.name, '-zoom', x, y)
  4239.         return destImage
  4240.  
  4241.     
  4242.     def subsample(self, x, y = ''):
  4243.         '''Return a new PhotoImage based on the same image as this widget
  4244.         but use only every Xth or Yth pixel.'''
  4245.         destImage = PhotoImage()
  4246.         if y == '':
  4247.             y = x
  4248.         
  4249.         self.tk.call(destImage, 'copy', self.name, '-subsample', x, y)
  4250.         return destImage
  4251.  
  4252.     
  4253.     def get(self, x, y):
  4254.         '''Return the color (red, green, blue) of the pixel at X,Y.'''
  4255.         return self.tk.call(self.name, 'get', x, y)
  4256.  
  4257.     
  4258.     def put(self, data, to = None):
  4259.         '''Put row formated colors to image starting from
  4260.         position TO, e.g. image.put("{red green} {blue yellow}", to=(4,6))'''
  4261.         args = (self.name, 'put', data)
  4262.         if to:
  4263.             if to[0] == '-to':
  4264.                 to = to[1:]
  4265.             
  4266.             args = args + ('-to',) + tuple(to)
  4267.         
  4268.         self.tk.call(args)
  4269.  
  4270.     
  4271.     def write(self, filename, format = None, from_coords = None):
  4272.         '''Write image to file FILENAME in FORMAT starting from
  4273.         position FROM_COORDS.'''
  4274.         args = (self.name, 'write', filename)
  4275.         if format:
  4276.             args = args + ('-format', format)
  4277.         
  4278.         if from_coords:
  4279.             args = args + ('-from',) + tuple(from_coords)
  4280.         
  4281.         self.tk.call(args)
  4282.  
  4283.  
  4284.  
  4285. class BitmapImage(Image):
  4286.     '''Widget which can display a bitmap.'''
  4287.     
  4288.     def __init__(self, name = None, cnf = { }, master = None, **kw):
  4289.         '''Create a bitmap with NAME.
  4290.  
  4291.         Valid resource names: background, data, file, foreground, maskdata, maskfile.'''
  4292.         Image.__init__(self, 'bitmap', name, cnf, master, **kw)
  4293.  
  4294.  
  4295.  
  4296. def image_names():
  4297.     return _default_root.tk.call('image', 'names')
  4298.  
  4299.  
  4300. def image_types():
  4301.     return _default_root.tk.call('image', 'types')
  4302.  
  4303.  
  4304. class Spinbox(Widget):
  4305.     '''spinbox widget.'''
  4306.     
  4307.     def __init__(self, master = None, cnf = { }, **kw):
  4308.         '''Construct a spinbox widget with the parent MASTER.
  4309.  
  4310.         STANDARD OPTIONS
  4311.  
  4312.             activebackground, background, borderwidth,
  4313.             cursor, exportselection, font, foreground,
  4314.             highlightbackground, highlightcolor,
  4315.             highlightthickness, insertbackground,
  4316.             insertborderwidth, insertofftime,
  4317.             insertontime, insertwidth, justify, relief,
  4318.             repeatdelay, repeatinterval,
  4319.             selectbackground, selectborderwidth
  4320.             selectforeground, takefocus, textvariable
  4321.             xscrollcommand.
  4322.  
  4323.         WIDGET-SPECIFIC OPTIONS
  4324.  
  4325.             buttonbackground, buttoncursor,
  4326.             buttondownrelief, buttonuprelief,
  4327.             command, disabledbackground,
  4328.             disabledforeground, format, from,
  4329.             invalidcommand, increment,
  4330.             readonlybackground, state, to,
  4331.             validate, validatecommand values,
  4332.             width, wrap,
  4333.         '''
  4334.         Widget.__init__(self, master, 'spinbox', cnf, kw)
  4335.  
  4336.     
  4337.     def bbox(self, index):
  4338.         '''Return a tuple of X1,Y1,X2,Y2 coordinates for a
  4339.         rectangle which encloses the character given by index.
  4340.  
  4341.         The first two elements of the list give the x and y
  4342.         coordinates of the upper-left corner of the screen
  4343.         area covered by the character (in pixels relative
  4344.         to the widget) and the last two elements give the
  4345.         width and height of the character, in pixels. The
  4346.         bounding box may refer to a region outside the
  4347.         visible area of the window.
  4348.         '''
  4349.         return self.tk.call(self._w, 'bbox', index)
  4350.  
  4351.     
  4352.     def delete(self, first, last = None):
  4353.         """Delete one or more elements of the spinbox.
  4354.  
  4355.         First is the index of the first character to delete,
  4356.         and last is the index of the character just after
  4357.         the last one to delete. If last isn't specified it
  4358.         defaults to first+1, i.e. a single character is
  4359.         deleted.  This command returns an empty string.
  4360.         """
  4361.         return self.tk.call(self._w, 'delete', first, last)
  4362.  
  4363.     
  4364.     def get(self):
  4365.         """Returns the spinbox's string"""
  4366.         return self.tk.call(self._w, 'get')
  4367.  
  4368.     
  4369.     def icursor(self, index):
  4370.         '''Alter the position of the insertion cursor.
  4371.  
  4372.         The insertion cursor will be displayed just before
  4373.         the character given by index. Returns an empty string
  4374.         '''
  4375.         return self.tk.call(self._w, 'icursor', index)
  4376.  
  4377.     
  4378.     def identify(self, x, y):
  4379.         '''Returns the name of the widget at position x, y
  4380.  
  4381.         Return value is one of: none, buttondown, buttonup, entry
  4382.         '''
  4383.         return self.tk.call(self._w, 'identify', x, y)
  4384.  
  4385.     
  4386.     def index(self, index):
  4387.         '''Returns the numerical index corresponding to index
  4388.         '''
  4389.         return self.tk.call(self._w, 'index', index)
  4390.  
  4391.     
  4392.     def insert(self, index, s):
  4393.         '''Insert string s at index
  4394.  
  4395.          Returns an empty string.
  4396.         '''
  4397.         return self.tk.call(self._w, 'insert', index, s)
  4398.  
  4399.     
  4400.     def invoke(self, element):
  4401.         '''Causes the specified element to be invoked
  4402.  
  4403.         The element could be buttondown or buttonup
  4404.         triggering the action associated with it.
  4405.         '''
  4406.         return self.tk.call(self._w, 'invoke', element)
  4407.  
  4408.     
  4409.     def scan(self, *args):
  4410.         '''Internal function.'''
  4411.         if not self._getints(self.tk.call((self._w, 'scan') + args)):
  4412.             pass
  4413.         return ()
  4414.  
  4415.     
  4416.     def scan_mark(self, x):
  4417.         '''Records x and the current view in the spinbox window;
  4418.  
  4419.         used in conjunction with later scan dragto commands.
  4420.         Typically this command is associated with a mouse button
  4421.         press in the widget. It returns an empty string.
  4422.         '''
  4423.         return self.scan('mark', x)
  4424.  
  4425.     
  4426.     def scan_dragto(self, x):
  4427.         '''Compute the difference between the given x argument
  4428.         and the x argument to the last scan mark command
  4429.  
  4430.         It then adjusts the view left or right by 10 times the
  4431.         difference in x-coordinates. This command is typically
  4432.         associated with mouse motion events in the widget, to
  4433.         produce the effect of dragging the spinbox at high speed
  4434.         through the window. The return value is an empty string.
  4435.         '''
  4436.         return self.scan('dragto', x)
  4437.  
  4438.     
  4439.     def selection(self, *args):
  4440.         '''Internal function.'''
  4441.         if not self._getints(self.tk.call((self._w, 'selection') + args)):
  4442.             pass
  4443.         return ()
  4444.  
  4445.     
  4446.     def selection_adjust(self, index):
  4447.         """Locate the end of the selection nearest to the character
  4448.         given by index,
  4449.  
  4450.         Then adjust that end of the selection to be at index
  4451.         (i.e including but not going beyond index). The other
  4452.         end of the selection is made the anchor point for future
  4453.         select to commands. If the selection isn't currently in
  4454.         the spinbox, then a new selection is created to include
  4455.         the characters between index and the most recent selection
  4456.         anchor point, inclusive. Returns an empty string.
  4457.         """
  4458.         return self.selection('adjust', index)
  4459.  
  4460.     
  4461.     def selection_clear(self):
  4462.         """Clear the selection
  4463.  
  4464.         If the selection isn't in this widget then the
  4465.         command has no effect. Returns an empty string.
  4466.         """
  4467.         return self.selection('clear')
  4468.  
  4469.     
  4470.     def selection_element(self, element = None):
  4471.         '''Sets or gets the currently selected element.
  4472.  
  4473.         If a spinbutton element is specified, it will be
  4474.         displayed depressed
  4475.         '''
  4476.         return self.selection('element', element)
  4477.  
  4478.  
  4479.  
  4480. class LabelFrame(Widget):
  4481.     '''labelframe widget.'''
  4482.     
  4483.     def __init__(self, master = None, cnf = { }, **kw):
  4484.         '''Construct a labelframe widget with the parent MASTER.
  4485.  
  4486.         STANDARD OPTIONS
  4487.  
  4488.             borderwidth, cursor, font, foreground,
  4489.             highlightbackground, highlightcolor,
  4490.             highlightthickness, padx, pady, relief,
  4491.             takefocus, text
  4492.  
  4493.         WIDGET-SPECIFIC OPTIONS
  4494.  
  4495.             background, class, colormap, container,
  4496.             height, labelanchor, labelwidget,
  4497.             visual, width
  4498.         '''
  4499.         Widget.__init__(self, master, 'labelframe', cnf, kw)
  4500.  
  4501.  
  4502.  
  4503. class PanedWindow(Widget):
  4504.     '''panedwindow widget.'''
  4505.     
  4506.     def __init__(self, master = None, cnf = { }, **kw):
  4507.         '''Construct a panedwindow widget with the parent MASTER.
  4508.  
  4509.         STANDARD OPTIONS
  4510.  
  4511.             background, borderwidth, cursor, height,
  4512.             orient, relief, width
  4513.  
  4514.         WIDGET-SPECIFIC OPTIONS
  4515.  
  4516.             handlepad, handlesize, opaqueresize,
  4517.             sashcursor, sashpad, sashrelief,
  4518.             sashwidth, showhandle,
  4519.         '''
  4520.         Widget.__init__(self, master, 'panedwindow', cnf, kw)
  4521.  
  4522.     
  4523.     def add(self, child, **kw):
  4524.         '''Add a child widget to the panedwindow in a new pane.
  4525.  
  4526.         The child argument is the name of the child widget
  4527.         followed by pairs of arguments that specify how to
  4528.         manage the windows. Options may have any of the values
  4529.         accepted by the configure subcommand.
  4530.         '''
  4531.         self.tk.call((self._w, 'add', child) + self._options(kw))
  4532.  
  4533.     
  4534.     def remove(self, child):
  4535.         '''Remove the pane containing child from the panedwindow
  4536.  
  4537.         All geometry management options for child will be forgotten.
  4538.         '''
  4539.         self.tk.call(self._w, 'forget', child)
  4540.  
  4541.     forget = remove
  4542.     
  4543.     def identify(self, x, y):
  4544.         '''Identify the panedwindow component at point x, y
  4545.  
  4546.         If the point is over a sash or a sash handle, the result
  4547.         is a two element list containing the index of the sash or
  4548.         handle, and a word indicating whether it is over a sash
  4549.         or a handle, such as {0 sash} or {2 handle}. If the point
  4550.         is over any other part of the panedwindow, the result is
  4551.         an empty list.
  4552.         '''
  4553.         return self.tk.call(self._w, 'identify', x, y)
  4554.  
  4555.     
  4556.     def proxy(self, *args):
  4557.         '''Internal function.'''
  4558.         if not self._getints(self.tk.call((self._w, 'proxy') + args)):
  4559.             pass
  4560.         return ()
  4561.  
  4562.     
  4563.     def proxy_coord(self):
  4564.         '''Return the x and y pair of the most recent proxy location
  4565.         '''
  4566.         return self.proxy('coord')
  4567.  
  4568.     
  4569.     def proxy_forget(self):
  4570.         '''Remove the proxy from the display.
  4571.         '''
  4572.         return self.proxy('forget')
  4573.  
  4574.     
  4575.     def proxy_place(self, x, y):
  4576.         '''Place the proxy at the given x and y coordinates.
  4577.         '''
  4578.         return self.proxy('place', x, y)
  4579.  
  4580.     
  4581.     def sash(self, *args):
  4582.         '''Internal function.'''
  4583.         if not self._getints(self.tk.call((self._w, 'sash') + args)):
  4584.             pass
  4585.         return ()
  4586.  
  4587.     
  4588.     def sash_coord(self, index):
  4589.         '''Return the current x and y pair for the sash given by index.
  4590.  
  4591.         Index must be an integer between 0 and 1 less than the
  4592.         number of panes in the panedwindow. The coordinates given are
  4593.         those of the top left corner of the region containing the sash.
  4594.         pathName sash dragto index x y This command computes the
  4595.         difference between the given coordinates and the coordinates
  4596.         given to the last sash coord command for the given sash. It then
  4597.         moves that sash the computed difference. The return value is the
  4598.         empty string.
  4599.         '''
  4600.         return self.sash('coord', index)
  4601.  
  4602.     
  4603.     def sash_mark(self, index):
  4604.         '''Records x and y for the sash given by index;
  4605.  
  4606.         Used in conjunction with later dragto commands to move the sash.
  4607.         '''
  4608.         return self.sash('mark', index)
  4609.  
  4610.     
  4611.     def sash_place(self, index, x, y):
  4612.         '''Place the sash given by index at the given coordinates
  4613.         '''
  4614.         return self.sash('place', index, x, y)
  4615.  
  4616.     
  4617.     def panecget(self, child, option):
  4618.         '''Query a management option for window.
  4619.  
  4620.         Option may be any value allowed by the paneconfigure subcommand
  4621.         '''
  4622.         return self.tk.call((self._w, 'panecget') + (child, '-' + option))
  4623.  
  4624.     
  4625.     def paneconfigure(self, tagOrId, cnf = None, **kw):
  4626.         '''Query or modify the management options for window.
  4627.  
  4628.         If no option is specified, returns a list describing all
  4629.         of the available options for pathName.  If option is
  4630.         specified with no value, then the command returns a list
  4631.         describing the one named option (this list will be identical
  4632.         to the corresponding sublist of the value returned if no
  4633.         option is specified). If one or more option-value pairs are
  4634.         specified, then the command modifies the given widget
  4635.         option(s) to have the given value(s); in this case the
  4636.         command returns an empty string. The following options
  4637.         are supported:
  4638.  
  4639.         after window
  4640.             Insert the window after the window specified. window
  4641.             should be the name of a window already managed by pathName.
  4642.         before window
  4643.             Insert the window before the window specified. window
  4644.             should be the name of a window already managed by pathName.
  4645.         height size
  4646.             Specify a height for the window. The height will be the
  4647.             outer dimension of the window including its border, if
  4648.             any. If size is an empty string, or if -height is not
  4649.             specified, then the height requested internally by the
  4650.             window will be used initially; the height may later be
  4651.             adjusted by the movement of sashes in the panedwindow.
  4652.             Size may be any value accepted by Tk_GetPixels.
  4653.         minsize n
  4654.             Specifies that the size of the window cannot be made
  4655.             less than n. This constraint only affects the size of
  4656.             the widget in the paned dimension -- the x dimension
  4657.             for horizontal panedwindows, the y dimension for
  4658.             vertical panedwindows. May be any value accepted by
  4659.             Tk_GetPixels.
  4660.         padx n
  4661.             Specifies a non-negative value indicating how much
  4662.             extra space to leave on each side of the window in
  4663.             the X-direction. The value may have any of the forms
  4664.             accepted by Tk_GetPixels.
  4665.         pady n
  4666.             Specifies a non-negative value indicating how much
  4667.             extra space to leave on each side of the window in
  4668.             the Y-direction. The value may have any of the forms
  4669.             accepted by Tk_GetPixels.
  4670.         sticky style
  4671.             If a window\'s pane is larger than the requested
  4672.             dimensions of the window, this option may be used
  4673.             to position (or stretch) the window within its pane.
  4674.             Style is a string that contains zero or more of the
  4675.             characters n, s, e or w. The string can optionally
  4676.             contains spaces or commas, but they are ignored. Each
  4677.             letter refers to a side (north, south, east, or west)
  4678.             that the window will "stick" to. If both n and s
  4679.             (or e and w) are specified, the window will be
  4680.             stretched to fill the entire height (or width) of
  4681.             its cavity.
  4682.         width size
  4683.             Specify a width for the window. The width will be
  4684.             the outer dimension of the window including its
  4685.             border, if any. If size is an empty string, or
  4686.             if -width is not specified, then the width requested
  4687.             internally by the window will be used initially; the
  4688.             width may later be adjusted by the movement of sashes
  4689.             in the panedwindow. Size may be any value accepted by
  4690.             Tk_GetPixels.
  4691.  
  4692.         '''
  4693.         if cnf is None and not kw:
  4694.             cnf = { }
  4695.             for x in self.tk.split(self.tk.call(self._w, 'paneconfigure', tagOrId)):
  4696.                 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
  4697.             
  4698.             return cnf
  4699.         
  4700.         if type(cnf) == StringType and not kw:
  4701.             x = self.tk.split(self.tk.call(self._w, 'paneconfigure', tagOrId, '-' + cnf))
  4702.             return (x[0][1:],) + x[1:]
  4703.         
  4704.         self.tk.call((self._w, 'paneconfigure', tagOrId) + self._options(cnf, kw))
  4705.  
  4706.     paneconfig = paneconfigure
  4707.     
  4708.     def panes(self):
  4709.         '''Returns an ordered list of the child panes.'''
  4710.         return self.tk.call(self._w, 'panes')
  4711.  
  4712.  
  4713.  
  4714. class Studbutton(Button):
  4715.     
  4716.     def __init__(self, master = None, cnf = { }, **kw):
  4717.         Widget.__init__(self, master, 'studbutton', cnf, kw)
  4718.         self.bind('<Any-Enter>', self.tkButtonEnter)
  4719.         self.bind('<Any-Leave>', self.tkButtonLeave)
  4720.         self.bind('<1>', self.tkButtonDown)
  4721.         self.bind('<ButtonRelease-1>', self.tkButtonUp)
  4722.  
  4723.  
  4724.  
  4725. class Tributton(Button):
  4726.     
  4727.     def __init__(self, master = None, cnf = { }, **kw):
  4728.         Widget.__init__(self, master, 'tributton', cnf, kw)
  4729.         self.bind('<Any-Enter>', self.tkButtonEnter)
  4730.         self.bind('<Any-Leave>', self.tkButtonLeave)
  4731.         self.bind('<1>', self.tkButtonDown)
  4732.         self.bind('<ButtonRelease-1>', self.tkButtonUp)
  4733.         self['fg'] = self['bg']
  4734.         self['activebackground'] = self['bg']
  4735.  
  4736.  
  4737.  
  4738. def _test():
  4739.     root = Tk()
  4740.     text = 'This is Tcl/Tk version %s' % TclVersion
  4741.     if TclVersion >= 8.0999999999999996:
  4742.         
  4743.         try:
  4744.             text = text + unicode('\nThis should be a cedilla: \xe7', 'iso-8859-1')
  4745.         except NameError:
  4746.             pass
  4747.         except:
  4748.             None<EXCEPTION MATCH>NameError
  4749.         
  4750.  
  4751.     None<EXCEPTION MATCH>NameError
  4752.     label = Label(root, text = text)
  4753.     label.pack()
  4754.     test = Button(root, text = 'Click me!', command = (lambda root = root: root.test.configure(text = '[%s]' % root.test['text'])))
  4755.     test.pack()
  4756.     root.test = test
  4757.     quit = Button(root, text = 'QUIT', command = root.destroy)
  4758.     quit.pack()
  4759.     root.iconify()
  4760.     root.update()
  4761.     root.deiconify()
  4762.     root.mainloop()
  4763.  
  4764. if __name__ == '__main__':
  4765.     _test()
  4766.  
  4767.